《Python核心編程》第二版第230頁第九章練習 -Python核心編程答案-自己做的-

來源:互聯網
上載者:User

本部落格列出的答案不是來自官方資源,是我自己做的練習,可能有誤。

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.