os 和 os.path 模块例子

import os

for tmpdir in ('/tmp', r'c:\temp'):
if os.path.isdir(tmpdir):
break
else:
print 'no temp directory available'

tmpdir = ''
if tmpdir:
os.chdir(tmpdir)
cwd = os.getcwd()
print '*** current temporary directory'
print cwd
print '*** creating example directory...'
os.mkdir('example')
os.chdir('example')
cwd = os.getcwd()
print '*** new working directory:'
print cwd
print '*** original directory listing:'
print os.listdir(cwd)
print '*** creating test file...'
fobj = open('test', 'w')
fobj.write('foo\n')
fobj.write('bar\n')
fobj.close()
print '*** updated directory listing:'
print os.listdir(cwd)

print "*** renaming 'test' to 'filetest.txt'"
os.rename('test', 'filetest.txt')
print '*** updated directory listing:'
print os.listdir(cwd)

path = os.path.join(cwd, os.listdir (cwd)[0])
print '*** full file pathname'
print path
print '*** (pathname, basename) =='
print os.path.split(path)
print '*** (filename, extension) =='
print os.path.splitext(os.path.basename(path))

print '*** displaying file contents:'
fobj = open(path)
for eachLine in fobj:
print eachLine
fobj.close()

print '*** deleting test file'
os.remove(path)
print '*** updated directory listing:'
print os.listdir(cwd)
os.chdir(os.pardir)
print '*** deleting test directory'
os.rmdir('example')
print '*** DONE'

  • 在 DOS 窗口下执行这个例子输出如下:
    *** current temporary directory
    C:\Users\gaolihong\Desktop\exercise
    *** creating example directory...
    *** new working directory:
    C:\Users\gaolihong\Desktop\exercise\example
    *** original directory listing:
    []
    *** creating test file...
    *** updated directory listing:
    ['test']
    *** renaming 'test' to 'filetest.txt'
    *** updated directory listing:
    ['filetest.txt']
    *** full file pathname
    C:\Users\gaolihong\Desktop\exercise\example\filetest.txt
    *** (pathname, basename) ==
    ('C:\Users\gaolihong\Desktop\exercise\example', 'filetest.txt')
    *** (filename, extension) ==
    ('filetest', '.txt')
    *** displaying file contents:
    foo

bar

*** deleting test file
*** updated directory listing:
[]
*** deleting test directory
*** DONE
[Finished in 0.1s]

你可能感兴趣的:(os 和 os.path 模块例子)