import os.path
for path in [ '/one/two/three',
'/one/two/three/',
'/',
'.',
'']:
print 's : %s' % (path, os.path.split(path))
/one/two/three : ('/one/two', 'three')
/one/two/three/ : ('/one/two/three', '')
/ : ('/', '')
. : ('', '.')
: ('', '')
import os.path
for path in [ '/one/two/three',
'/one/two/three/',
'/',
'.',
'']:
print 's : %s' % (path, os.path.basename(path))
/one/two/three : three
/one/two/three/ :
/ :
. : .
:
import os.path
for path in [ '/one/two/three',
'/one/two/three/',
'/',
'.',
'']:
print 's : %s' % (path, os.path.dirname(path))
/one/two/three : /one/two
/one/two/three/ : /one/two/three
/ : /
. :
:
for path in [ '/one.txt',
'/one/two/three.txt',
'/',
'.',
''
'two.tar.gz']:
print '%21s : %s' % (path, os.path.splitext(path))
/one.txt : ('/one', '.txt')
/one/two/three.txt : ('/one/two/three', '.txt')
/ : ('/', '')
. : ('.', '')
two.tar.gz : ('two.tar', '.gz')
import os.path
paths = [ '/one/two/three',
'/one/two/threetxt',
'/one/two/three/four',]
for path in paths:
print 'PATH:', path
print
print 'PREFIX:', os.path.commonprefix(paths)
PATH: /one/two/three PATH: /one/two/threetxt PATH: /one/two/three/four PREFIX: /one/two/three
import os.path
for parts in [ ('one', 'two', 'three'),
('\one', 'two', 'three'),
('/one', '/two', '/three', '/four'),]:
print parts, ':', os.path.join(*parts)
('one', 'two', 'three') : one\two\three
('\\one', 'two', 'three') : \one\two\three
('/one', '/two', '/three', '/four') : /four
import os.path
for path in [ 'one/two/three',
'one/./two/three',
'one/../alt/two/three',
]:
print '%20s : %s' % (path, os.path.normpath(path))
one/two/three : one\two\three
one/./two/three : one\two\three
one/../alt/two/three : alt\two\three
import os.path
for path in [ '.',
'..',
'one/two/three',
'one/./two/three',
'one/../alt/two/three',
]:
print '%20s : %s' % (path, os.path.abspath(path))
. : C:\Users\Administrator\Desktop
.. : C:\Users\Administrator
one/two/three : C:\Users\Administrator\Desktop\one\two\three
one/./two/three : C:\Users\Administrator\Desktop\one\two\three
one/../alt/two/three : C:\Users\Administrator\Desktop\alt\two\three
import os import time print 'File:', __file__ print 'Access time:', time.ctime(os.path.getatime(__file__)) print 'Modified time:', time.ctime(os.path.getmtime(__file__)) print 'Change time:', time.ctime(os.path.getctime(__time__)) print 'Size:', os.path.getsize(__file__)
import os.path filename = r'C:\Users\Administrator\Desktop\tmp' print 'File :', filename print 'Is file? :', os.path.isfile(filename) print 'Absoulute :', os.path.isabs(filename) print 'Is dir? :', os.path.isdir(filename) print 'Is link? :', os.path.islink(filename) print 'Mountpoint? :', os.path.ismount(filename) print 'Exists? :', os.path.exists(filename) print 'Link Exists? :', os.path.lexists(filename)
File : C:\Users\Administrator\Desktop\tmp Is file? : False Absoulute : True Is dir? : True Is link? : False Mountpoint? : False Exists? : True Link Exists? : True
import os
import os.path
import pprint
def visit(arg, dirname, names):
print dirname, arg
for name in names:
subname = os.path.join(dirname, name)
if os.path.isdir(subname):
print '%s/' % name
else:
print ' %s' % name
print
if not os.path.exists('example'):
os.mkdir('example')
if not os.path.exists('example/one'):
os.mkdir('example/one')
with open('example/one/file.txt', 'wt') as f:
f.write('i love you')
with open('example/one/another.txt', 'wt') as f:
f.write('i love you, two')
os.path.walk('example', visit, '(User data)')
example (User data) one/ example\one (User data) another.txt file.txt
#创建文件:
os.mknod("test.txt") 创建空文件
fp = open("test.txt",w) 直接打开一个文件,如果文件不存在则创建文件
#获取扩展名:
>>> os.path.splitext('/Volumes/Leopard/Users/Caroline/Desktop/1.mp4')[1:]
('.mp4',)
>>> os.path.splitext('/Volumes/Leopard/Users/Caroline/Desktop/1.mp4')[1]
'.mp4'
#获取文件名:
>>> print os.path.basename(r'/root/hahaha/123.txt')
123.txt
>>> print os.path.dirname(r'/root/hahaha/123.txt')
/root/hahaha
#判断目录或文件的存在:
>>> os.path.exists('/root/1.py')
True
>>> os.path.exists('/root/')
True
>>> os.path.exists('/root')
True
>>> os.path.isdir('/root')
True
#改变工作目录:
>>> os.chdir('/home')
>>> os.getcwd()
'/home'
#字符串分割:
>>> '/usr/bin/env'.split('/')
['', 'usr', 'bin', 'env']
#获取文件夹大小(Python2.x):
import os
from os.path import join, getsize
def getdirsize(dir):
size = 0L
for root, dirs, files in os.walk(dir):
size += sum([getsize(join(root, name)) for name in files])
return size
if __name__ == '__main__':
filesize = getdirsize('/tmp')
print 'There are %.3f' % (filesize/1024/1024), 'Mbytes in /tmp'
#获取文件夹大小(Python3.x):
import os
from os.path import join, getsize
def getdirsize(dir):
size = 0
for root, dirs, files in os.walk(dir):
size += sum([getsize(join(root, name)) for name in files])
return size
if __name__ == '__main__':
filesize = getdirsize('/tmp')
print ('There are ' + str(filesize/1024/1024) + 'Mbytes in /tmp')
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有