Pythonファイルシステム-os.path
7240 ワード
6.1 os.path——
, , 。
6.1.1
os :
os.sep- 。
os.extsep- 。
os.pardir- 。
os.curdir- 。
split() , tuple。 , 。
import os.path
for path in [ '/one/two/three',
'/one/two/three/',
'/',
'.',
'']:
print '%15s : %s' % (path, os.path.split(path))
os.sep , 。
>>> ================================ RESTART ================================
>>>
/one/two/three : ('/one/two', 'three')
/one/two/three/ : ('/one/two/three', '')
/ : ('/', '')
. : ('', '.')
: ('', '')
basename() split() 。
import os.path
for path in [ '/one/two/three',
'/one/two/three/',
'/',
'.',
'']:
print '%15s : %s' % (path, os.path.basename(path))
。
>>> ================================ RESTART ================================
>>>
/one/two/three : three
/one/two/three/ :
/ :
. : .
:
dirname() 。
import os.path
for path in [ '/one/two/three',
'/one/two/three/',
'/',
'.',
'']:
print '%15s : %s' % (path, os.path.dirname(path))
basename() dirname() , 。
>>> ================================ RESTART ================================
>>>
/one/two/three : /one/two
/one/two/three/ : /one/two/three
/ : /
. :
:
splitext() split(), 。
import os.path
for path in [ '/one.txt',
'/one/two/three.txt',
'/',
'.',
''
'two.tar.gz']:
print '%21s : %s' % (path, os.path.splitext(path))
, os.extsep 。
>>> ================================ RESTART ================================
>>>
/one.txt : ('/one', '.txt')
/one/two/three.txt : ('/one/two/three', '.txt')
/ : ('/', '')
. : ('.', '')
two.tar.gz : ('two.tar', '.gz')
commonprefix() , , 。
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)
>>> ================================ RESTART ================================
>>>
PATH: /one/two/three
PATH: /one/two/threetxt
PATH: /one/two/three/four
PREFIX: /one/two/three
6.1.2
, , join()。
import os.path
for parts in [ ('one', 'two', 'three'),
('\one', 'two', 'three'),
('/one', '/two', '/three', '/four'),]:
print parts, ':', os.path.join(*parts)
os.sep , , 。
>>> ================================ RESTART ================================
>>>
('one', 'two', 'three') : one\two\three
('\\one', 'two', 'three') : \one\two\three
('/one', '/two', '/three', '/four') : /four
6.1.3
join() , , normpath() 。
import os.path
for path in [ 'one/two/three',
'one/./two/three',
'one/../alt/two/three',
]:
print '%20s : %s' % (path, os.path.normpath(path))
os.curdir os.pardir 。
>>> ================================ RESTART ================================
>>>
one/two/three : one\two\three
one/./two/three : one\two\three
one/../alt/two/three : alt\two\three
, abspath()。
import os.path
for path in [ '.',
'..',
'one/two/three',
'one/./two/three',
'one/../alt/two/three',
]:
print '%20s : %s' % (path, os.path.abspath(path))
。
>>> ================================ RESTART ================================
>>>
. : 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
6.1.4
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__)
, , , 。
6.1.5
, 。
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)
。
>>> ================================ RESTART ================================
>>>
File : C:\Users\Administrator\Desktop\tmp
Is file? : False
Absoulute : True
Is dir? : True
Is link? : False
Mountpoint? : False
Exists? : True
Link Exists? : True
6.1.6
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)')
。
>>> ================================ RESTART ================================
>>>
example (User data)
one/
example\one (User data)
another.txt
file.txt