本博客列出的答案不是来自官方资源,是我自己做的练习,可能有误。
9-1.文件过滤。显示一个文件的所有行,忽略以井号(#)开头的行。这个字符被用做Python,Perl,Tcl,等大多数脚本文件的注释符号。附加题:处理不是第一个字符开头的注释。
【答案】
(a)代码如下:
fobj = open('c:\Python Test\P_1.txt') for eachLine in fobj: if eachLine[0] != '#': print eachLine, fobj.close()
文件P_1.txt具体是:
apple
banana
#orange
orange#
orange#orange
pie
donut
【执行结果】
apple
banana
orange#
orange#orange
pie
donut
(b)附加题代码如下:
fobj = open('c:\Python Test\P_1.txt') for eachLine in fobj: switch = True for i in eachLine[1:]: if i == '#': switch = False if switch: print eachLine, fobj.close()
【执行结果】
apple
banana
#orange
pie
donut
9-2.文件访问。提示输入数字N和文件F,然后显示文件的前N行。
【答案】
代码如下:
N = raw_input("Please input the line numbers: ... ") F = raw_input("Please input the full location and file name: ... ") fobj = open(F) k = fobj.readlines() for i in k[:int(N)]: print i, fobj.close()
【参考】
http://www.czug.org/python/pyessentialref/09.htm
9-3.文件信息,提示输入一个文件名,然后显示这个文本文件的总行数。
【答案】
代码如下:
filename = raw_input("Please input the file location and name: ... ") fobj = open(filename) print len(fobj.readlines()) fobj.close() *** *** *** *** Example 9.1. os & os.path Modules Example (ospathex.py) 【例子源码】 import os for tmpdir in ('/tmp', r'c:\Python Test'): 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'
【执行结果】
*** current temporary directory
c:\Python Test
*** creating example directory...
*** new working directory:
c:\Python Test\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:\Python Test\example\filetest.txt
*** (pathname, basename) ==
('c:\\Python Test\\example', 'filetest.txt')
*** (filename, extension) ==
('filetest', '.txt')
*** displaying file contents:
foo
bar
*** deleting test file
*** updated directory listing:
[]
*** deleting test directory
*** DONE