整理了一下python 中文件的輸入輸出及主要介紹一些os模塊中對(duì)文件系統(tǒng)的操作。
文件輸入輸出
1、內(nèi)建函數(shù)open(file_name,文件打開(kāi)模式,通用換行符支持),打開(kāi)文件返回文件對(duì)象。
2、對(duì)打開(kāi)文件進(jìn)行讀取時(shí),readline()與readlines()的區(qū)別在于是否一次性的讀取所有的內(nèi)容,并將每行的信息作為列表中的一個(gè)子項(xiàng)。
例如:文件test.txt中
1,3,4
2,35,6
分別用readline與readlines對(duì)其進(jìn)行讀取
r=file_object.readline();#結(jié)果為1,3,4r=file_object.readlines();#結(jié)果為['1,3,4/n', '2,35,6']
3、文件迭代
使用迭代器的file.next()用于讀取文件的下一行。相比f(wàn)or循環(huán),更復(fù)雜,一般采用 for循環(huán)直接迭代。
4、文件移動(dòng)
seek(off,whence=0)可以在文件中移動(dòng)文件指針到不同的位置,,從文件中移動(dòng)off個(gè)操作標(biāo)記(文件指針),正往結(jié)束方向移動(dòng),負(fù)往開(kāi)始方向移動(dòng)。如果設(shè)定了whence參數(shù),就以whence設(shè)定的起始位為準(zhǔn),0代表從頭開(kāi)始,1代表當(dāng)前位置,2代表文件最末尾位置。
tell()可以展示 我們的移動(dòng)過(guò)程,展示我們的當(dāng)前位置
5、os模塊
6、文件寫(xiě)入f.write();writelines()接受一個(gè)字符串列表作為參數(shù)
需要手動(dòng)輸入換行符/n;
fobj=open('test','w');#直接在指定路徑下打開(kāi)test1 ,如果沒(méi)有則直接生成,但若存在,則出錯(cuò);fobj.write('foo/n');fobj.write('bar/n');fobj.close();#結(jié)果為#foo#barimport os;file_object=open(r'E:/Python/iostream_test/test.txt','r+');aline=raw_input("Enter a line ('.'to quit):");if aline !=".": file_object.write('%s%s' % (aline,os.linesep));#在文件test.txt中寫(xiě)入一條字符串結(jié)果為txt 文件中的一個(gè)內(nèi)容標(biāo)準(zhǔn)文件
一般程序一執(zhí)行,就可以訪問(wèn)3個(gè)標(biāo)準(zhǔn)文件,分別是標(biāo)準(zhǔn)輸入(一般是鍵盤(pán))、標(biāo)準(zhǔn)輸出(到顯示器的緩沖輸出)和標(biāo)準(zhǔn)錯(cuò)誤(到屏幕的非緩沖輸出),這里的緩沖、非緩沖是指open()的三個(gè)參數(shù)。
文件系統(tǒng)
對(duì)文件系統(tǒng)的訪問(wèn)大多通過(guò)python的os模塊實(shí)現(xiàn)。該模塊是python訪問(wèn)操作系統(tǒng)功能的主要接口。
os除了對(duì)進(jìn)程和進(jìn)程運(yùn)行環(huán)境進(jìn)行管理外,os模塊還負(fù)責(zé)處理大部分的文件系統(tǒng)操作,包括刪除/重命名文件,遍歷目錄樹(shù),已經(jīng)管理文件訪問(wèn)權(quán)限等。
另一個(gè)os.path 模塊可以完成針對(duì)路徑名的操作,它提供函數(shù) 完成管理和操作文件路徑中的各個(gè)部分,獲取文件或者子目錄信息,文件路徑查詢等操作。
針對(duì)os path的操作,操作對(duì)象E:/Python/iostream_test文件及其下的test.txt文件
os.path.exists(),檢測(cè)指定路徑的文件或者目錄是否存在。
import os;for tempdir in ('/test.txt',r'E:/Python/iostream_test/test.txt'): if os.path.exists(tempdir): print 'yes'; break;else: print 'no temp directory available'; tempdir=' ';#結(jié)果為yes,# 若in中改為('/test.txt',r'D:/Python/iostream_test/test.txt'),則結(jié)果為no temp directory availableos.path.isdir(),檢測(cè)指定了路徑是否存在且為一個(gè)目錄,只能是目錄,否則報(bào)錯(cuò)。import os;for tempdir in ('/test.txt',r'E:/Python/iostream_test/test.txt'): #in中檢測(cè)的是文件,而非目錄,所以未輸出yes if os.path.isdir(tempdir): print 'yes'; break;else: print 'no temp directory available'; tempdir=' ';# 輸出no temp directory availableimport os;for tempdir in ('/test.txt',r'D:/Python/iostream_test/test.txt'):#指定路徑在D盤(pán),因而不存在 if os.path.isdir(tempdir): print 'yes'; break;else: print 'no temp directory available'; tempdir=' ';import os;for tempdir in ('/test.txt',r'E:/Python/iostream_test'): if os.path.isdir(tempdir): print 'yes'; break;else: print 'no temp directory available'; tempdir=' ';#輸出的是yes 同理可得os.path.isfile()只可檢測(cè)指定路徑是否存在且為一個(gè)文件
以下針對(duì)os中某些進(jìn)行練習(xí),針對(duì)文件的操作,因先檢測(cè)是否存在指定路徑,再對(duì)該路徑或者路徑中的文件做操作。更多的練習(xí)可以看read.md
import os;for tempdir in ('/tmp',r'E:/Python/iostream_test'): if os.path.isdir(tempdir):#檢測(cè)指定路徑是否存在且為一個(gè)目錄,并賦給tempdir print 'yes'; break;else: print 'no temp directory available'; tempdir=' ';if tempdir: os.chdir(tempdir); #改變當(dāng)前工作路徑 cwd=os.getcwd(); #獲取當(dāng)前工作路徑; print 'current temporany directory is :'; print cwd; print os.listdir(cwd); print 'creating example directory'; os.mkdir('example'); #在當(dāng)前目錄下新建一個(gè)新的文件 os.chdir('example'); #改變目錄到example的文件下 cwd=os.getcwd();#獲取example的文件路徑 print 'new working directory:' print cwd; print ' original directory listing :' print os.listdir(cwd);#列出(example)指定路徑下的文件 os.chdir(tempdir); cwd=os.getcwd(); print os.listdir(cwd);#列出(tempdir)指定路徑下的文件# 結(jié)果為:# current temporany directory is :# E:/Python/iostream_test# ['pspathex.py', 'read.md', 'read.py', 'test.txt']# creating example directory# new working directory:# E:/Python/iostream_test/example# original directory listing :# []# ['example', 'pspathex.py', 'read.md', 'read.py', 'test.txt']os.path.join()方法將分離的各部分組合成一個(gè)路徑名 path=os.path.join(cwd,os.listdir(cwd)[0]); print ' full file pathname:' print path; #結(jié)果為E:/Python/iostream_test/example/filetest.txtos.path.split(path)方法將組合路徑分成(路徑名,文件名)path=os.path.join(cwd,os.listdir(cwd)[0]);print os.path.split(path);#(pathname,basename)#結(jié)果為('E://Python//iostream_test//example', 'filetest.txt')os.path.splitext(os.path.basename(path))方法將文件分成(文件名,文件擴(kuò)展名)path=os.path.join(cwd,os.listdir(cwd)[0]);print os.path.splitext(os.path.basename(path));#(filename,extension)#結(jié)果為('filetest', '.txt')相關(guān)模塊
永久存儲(chǔ)模塊,永久存儲(chǔ)數(shù)據(jù):pickle 、marshal模塊、DBM風(fēng)格模塊、shelve模塊
總結(jié)
以上所述是小編給大家介紹的python 中文件輸入輸出及os模塊對(duì)文件系統(tǒng)的操作方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VEVB武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選