python 中文件輸入輸出及os模塊對文件系統(tǒng)的操作方法
整理了一下python 中文件的輸入輸出及主要介紹一些os模塊中對文件系統(tǒng)的操作。
文件輸入輸出
1、內(nèi)建函數(shù)open(file_name,文件打開模式,通用換行符支持),打開文件返回文件對象。
2、對打開文件進(jìn)行讀取時,readline()與readlines()的區(qū)別在于是否一次性的讀取所有的內(nèi)容,并將每行的信息作為列表中的一個子項(xiàng)。
例如:文件test.txt中
1,3,4
2,35,6
分別用readline與readlines對其進(jìn)行讀取
r=file_object.readline(); #結(jié)果為1,3,4 r=file_object.readlines(); #結(jié)果為['1,3,4\n', '2,35,6']
3、文件迭代
使用迭代器的file.next()用于讀取文件的下一行。相比for循環(huán),更復(fù)雜,一般采用 for循環(huán)直接迭代。
4、文件移動
seek(off,whence=0)可以在文件中移動文件指針到不同的位置,,從文件中移動off個操作標(biāo)記(文件指針),正往結(jié)束方向移動,負(fù)往開始方向移動。如果設(shè)定了whence參數(shù),就以whence設(shè)定的起始位為準(zhǔn),0代表從頭開始,1代表當(dāng)前位置,2代表文件最末尾位置。
tell()可以展示 我們的移動過程,展示我們的當(dāng)前位置
5、os模塊
6、文件寫入f.write();writelines()接受一個字符串列表作為參數(shù)
需要手動輸入換行符\n;
fobj=open('test','w');#直接在指定路徑下打開test1 ,如果沒有則直接生成,但若存在,則出錯;
fobj.write('foo\n');
fobj.write('bar\n');
fobj.close();
#結(jié)果為
#foo
#bar
import 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中寫入一條字符串結(jié)果為txt 文件中的一個內(nèi)容
標(biāo)準(zhǔn)文件
一般程序一執(zhí)行,就可以訪問3個標(biāo)準(zhǔn)文件,分別是標(biāo)準(zhǔn)輸入(一般是鍵盤)、標(biāo)準(zhǔn)輸出(到顯示器的緩沖輸出)和標(biāo)準(zhǔn)錯誤(到屏幕的非緩沖輸出),這里的緩沖、非緩沖是指open()的三個參數(shù)。
文件系統(tǒng)
對文件系統(tǒng)的訪問大多通過python的os模塊實(shí)現(xiàn)。該模塊是python訪問操作系統(tǒng)功能的主要接口。
os除了對進(jìn)程和進(jìn)程運(yùn)行環(huán)境進(jìn)行管理外,os模塊還負(fù)責(zé)處理大部分的文件系統(tǒng)操作,包括刪除/重命名文件,遍歷目錄樹,已經(jīng)管理文件訪問權(quán)限等。
另一個os.path 模塊可以完成針對路徑名的操作,它提供函數(shù) 完成管理和操作文件路徑中的各個部分,獲取文件或者子目錄信息,文件路徑查詢等操作。
針對os path的操作,操作對象E:\Python\iostream_test文件及其下的test.txt文件
os.path.exists(),檢測指定路徑的文件或者目錄是否存在。
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 available
os.path.isdir(),檢測指定了路徑是否存在且為一個目錄,只能是目錄,否則報錯。
import os;
for tempdir in ('/test.txt',r'E:\Python\iostream_test\test.txt'):
#in中檢測的是文件,而非目錄,所以未輸出yes
if os.path.isdir(tempdir):
print 'yes';
break;
else:
print 'no temp directory available';
tempdir=' ';
# 輸出no temp directory available
import os;
for tempdir in ('/test.txt',r'D:\Python\iostream_test\test.txt'):
#指定路徑在D盤,因而不存在
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()只可檢測指定路徑是否存在且為一個文件
以下針對os中某些進(jìn)行練習(xí),針對文件的操作,因先檢測是否存在指定路徑,再對該路徑或者路徑中的文件做操作。更多的練習(xí)可以看read.md
import os;
for tempdir in ('/tmp',r'E:\Python\iostream_test'):
if os.path.isdir(tempdir):#檢測指定路徑是否存在且為一個目錄,并賦給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)前目錄下新建一個新的文件
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()方法將分離的各部分組合成一個路徑名
path=os.path.join(cwd,os.listdir(cwd)[0]);
print ' full file pathname:'
print path;
#結(jié)果為E:\Python\iostream_test\example\filetest.txt
os.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)模塊
永久存儲模塊,永久存儲數(shù)據(jù):pickle 、marshal模塊、DBM風(fēng)格模塊、shelve模塊
總結(jié)
以上所述是小編給大家介紹的python 中文件輸入輸出及os模塊對文件系統(tǒng)的操作方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Django實(shí)現(xiàn)簡單分頁功能的方法詳解
這篇文章主要介紹了Django實(shí)現(xiàn)簡單分頁功能的方法,結(jié)合實(shí)例形式分析了django的第三方模塊django-pure-pagination的安裝、使用及實(shí)現(xiàn)分頁的相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
django如何連接已存在數(shù)據(jù)的數(shù)據(jù)庫
這篇文章主要給大家介紹了關(guān)于django如何連接已存在數(shù)據(jù)的數(shù)據(jù)庫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用django具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
關(guān)于Python3的import問題(pycharm可以運(yùn)行命令行import錯誤)
這篇文章主要介紹了關(guān)于Python3的import問題(pycharm可以運(yùn)行命令行import錯誤),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
python實(shí)現(xiàn)應(yīng)用程序在右鍵菜單中添加打開方式功能
這篇文章主要介紹了python實(shí)現(xiàn)應(yīng)用程序在右鍵菜單中添加打開方式功能,本文分步驟給大家介紹的非常詳細(xì),具有參考借鑒價值,需要的朋友參考下吧2017-01-01

