本部落格列出的答案不是來自官方資源,是我自己做的練習,可能有誤。
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 osfor tmpdir in ('/tmp', r'c:\Python Test'): if os.path.isdir(tmpdir): breakelse: 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