日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

corepython第九章:文件和输入输出

發布時間:2023/11/27 生活经验 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 corepython第九章:文件和输入输出 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

學習筆記:

OS模塊代碼示例:

 1 import os
 2 for tmpdir in ('/tmp',r'c:\users\administrator\desktop'):
 3 #如果存在括號里面的目錄,則break
 4     if os.path.isdir(tmpdir):
 5         break
 6 #如果不存在,則tmpdir為空值,即False
 7 else:
 8     print 'no temp directory available'
 9     tmpdir=''
10 
11 if tmpdir:
12     os.chdir(tmpdir)    #將路徑change到這個目錄
13     cwd=os.getcwd() #將路徑名稱賦值給cwd
14     print '*** current temporary directory'
15     print cwd
16 
17     print '*** creating example directory'
18     os.mkdir('example') #在當前路徑下創建一個example的文件夾
19     os.chdir('example') #切換到這個文件夾下的路徑
20     cwd=os.getcwd() #將路徑賦值給cwd
21     print '*** new working directory:'
22     print cwd
23     print '*** original directory listing:'
24     print os.listdir(cwd) #print example文件夾下的文件名
25 
26     print'*** creating test file:'
27     fobj=open('test','w')   #在example文件夾下創建一個test的文件,以寫模式打開
28     fobj.write('foo\n')
29     fobj.write('bar\n')
30     fobj.close()
31     print '*** updated directory listing:'
32     print os.listdir(cwd)   #print example文件夾下的文件名
33 
34     os.rename('test','filetest.txt') #將test文件改名
35     print '*** updated directory listing:'
36     print os.listdir(cwd)   #print example文件夾下的文件名
37 
38     path=os.path.join(cwd,os.listdir(cwd)[0])   #將example文件夾路徑和filetest.txt文件名合并,也就是filetest.txt的完整路徑
39     print '*** fulle file pathname'
40     print path
41     print '*** (pathname,basename)=='
42     print os.path.split(path) #將路徑分開成文件夾路徑和文件名
43     print '**8 (filename,extension)=='
44     print os.path.splitext(os.path.basename(path))  #將文件名split生成一個元組,得到('filetest', '.txt')
45 
46     print '*** displaying file contents:'
47     fobj=open(path)
48     for eachline in fobj:
49         print eachline,
50     fobj.close()
51 
52     print '*** deleting test file'
53     os.remove(path) #刪除此文件,因為path是此文件的完整路徑
54     print '*** updated directory listing:'
55     print os.listdir(cwd) #此時應該為空
56     os.chdir(os.pardir) #返回當前路勁的父路徑(上一層路徑),也就是桌面
57     print '*** deleting test directory'
58     os.rmdir('example') #刪除example文件夾路徑
59     print '*** done'
View Code

課后習題

9-6.文件比較.寫一個比較兩個文本文件的程序,如果不同,給出第一個不同處的行號和列號.

 1 FA=raw_input("please input 1st file's path:")
 2 FB=raw_input("please input 2nd file's path:")
 3 file_a=open(FA,'r')
 4 file_b=open(FB,'r')
 5 a_rl=file_a.readlines()
 6 b_rl=file_b.readlines()
 7 file_a.close()
 8 file_b.close()
 9 MinRow=min(len(a_rl),len(b_rl)) #得到最小行數
10 for r in range(MinRow+1):
11     if a_rl[r]!=b_rl[r]:
12         print 'row:%d' % r+1
13         #得到第一個不同行的最小列數
14         MinCol=min(len(a_rl[r]),len(b_rl[r]))
15         for c in range(MinCol+1):
16             if a_rl[r][c] != b_rl[r][c]:
17                 print 'column:%d' % c+1
18                 break
19         break
View Code

9-9.python文檔字符串.進入python標準庫所在的目錄,檢查每個.py文件看是否有_doc_字符串,如果有,對格式進行適當的整理歸類.你的程序執行完畢后,應當生成一個漂亮的清單.里邊列出哪些模塊有文檔字符串,以及文檔字符串的內容,清單最后附上那些沒有文檔字符串模塊的名字.

 1 import os
 2 #標準庫的路徑
 3 dir='c:/python27/lib'
 4 #切換到此路徑
 5 os.chdir(dir)
 6 #此路徑下的所有文件的序列
 7 allfile=os.listdir(dir)
 8 #篩選出類型為py的文件
 9 allpy=[file for file in allfile if file[-3:]=='.py']
10 #定義字典和序列,將有doc的文件存入字典,沒有doc的文件存入序列
11 py_doc={}
12 py_no=[]
13 for lj in allpy:
14     f=open(lj,'r')
15     file=f.readlines()
16     #將第一行寫入doc
17     doc=file[0][3:]
18     #判定如果第一行的前三個字符為""",則有doc
19     if file[0][0:3]=='"""':
20         #將py的doc寫入doc字符串
21         for r in range(1,len(file)):
22             #如果遇到下一個""",則停止寫入doc字符串
23             if file[r][0:3]=='"""':
24                 break
25             doc+=file[r]
26         #以文件名為key,doc為value,存入字典
27         py_doc[lj]=doc
28     else:
29         #將沒有doc的文件加入到序列中
30         py_no.append(lj)
31 #輸出
32 print '***py has doc:'
33 for lj,doc in py_doc.items():
34     print '****************************************',lj,':'
35     print doc
36 print '***py has not doc:'
37 print ' '.join(py_no)
View Code

?

轉載于:https://www.cnblogs.com/alvysinger/p/4444205.html

總結

以上是生活随笔為你收集整理的corepython第九章:文件和输入输出的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。