国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

python 中文件輸入輸出及os模塊對(duì)文件系統(tǒng)的操作方法

2020-01-04 14:38:00
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

整理了一下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)站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 米泉市| 瑞丽市| 方正县| 全南县| 遵化市| 陵川县| 尼木县| 巴林左旗| 锡林郭勒盟| 保康县| 张北县| 邻水| 翁牛特旗| 西峡县| 靖远县| 灵寿县| 珲春市| 玛纳斯县| 互助| 湘潭县| 嘉兴市| 天柱县| 南通市| 黑龙江省| 仙桃市| 呼和浩特市| 葵青区| 瓦房店市| 西充县| 大关县| 太保市| 蒲城县| 唐山市| 株洲县| 宝山区| 历史| 龙江县| 蚌埠市| 巴东县| 恩平市| 浠水县|