1.文件的路徑

創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),囊謙企業(yè)網(wǎng)站建設(shè),囊謙品牌網(wǎng)站建設(shè),網(wǎng)站定制,囊謙網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,囊謙網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
(1) 相對(duì)路徑:文件所在路徑
(2) 絕對(duì)路徑:從根路徑起的目錄
2.文件的類型
(1)文本文件 : 可以直接編寫的文件
(2)二進(jìn)制文件: 安裝包 ,圖片
3.mode相關(guān)
(1)關(guān)于文件操作的mode
mode 打開文件的方式
r 讀
w 寫
x 創(chuàng)建并寫
a 追加
r+ 讀寫
w+ 寫讀
x+ 創(chuàng)建并寫讀
a+ 追加讀
(2)關(guān)于文件的mode
mode 文本類型
t 文本(默認(rèn))
b 二進(jìn)制
4.文件打開、讀取、關(guān)閉的過程
文件打開
open('path', 'mode')
>>> handler = open('tpl.xml', 'rt')文件讀取
>>> handler.read()
'<MySQL>\n <host>{HOST}</host>\n <port>{PORT}</port>\n <user>{USER}</user>\n</mysql>\n'文件關(guān)閉
>>> handler.close()
5.讀文件
handler.read() 讀取所有的內(nèi)容
handler.read(size) 讀取文件size個(gè)字節(jié)內(nèi)容
handler.readline() 讀取文件一行
handler.readlines() 讀取文件所有行
判斷文件是否讀完的依據(jù)是判斷最后一行是否為空
文件的可遍歷性
>>> handler = open('tpl.xml', 'rt')
>>> for lines in handler:
... print(lines)
...
<mysql>
<host>{HOST}</host>
<port>{PORT}</port>
<user>{USER}</user>
</mysql>
>>> handler.close()對(duì)于二進(jìn)制文件的讀方式 只有 read 和 read(size)
read方式讀取
>>> handler = open('1.py','rb')
>>> handler.read()
b'#!/usr/bin/python\n\na = 1\nb = 2\nc = [1,2]\n\ndef test():\n a = 4 \n b = 5\n c = []\n c.append(1)\n print(a,b,c)\n\ntest()\nprint(a,b,c)\n'
>>> handler.close()read(size)
>>> handler = open('1.py','rb')
>>> SIZE = 10
>>> while True:
... b_read = handler.read(SIZE)
... if b_read == b'':
... break
... print(b_read)
...
b'#!/usr/bin'
b'/python\n\na'
b' = 1\nb = 2'
b'\nc = [1,2]'
b'\n\ndef test'
b'():\n a '
b'= 4 \n b'
b' = 5\n c'
b' = []\n '
b'c.append(1'
b')\n prin'
b't(a,b,c)\n\n'
b'test()\npri'
b'nt(a,b,c)\n'6.字符串和二進(jìn)制字符串的轉(zhuǎn)換
字符串轉(zhuǎn)換成二進(jìn)制字符串
>>> "abc".encode() b'abc'
二進(jìn)制字符串轉(zhuǎn)換成字符串
>>> b'abc'.decode() 'abc'
7.寫文件
寫文件 (文件不存在的時(shí)候創(chuàng)建)
>>> handler = open('2.txt', 'wt')
>>> handler.write('123')
3
>>> handler.close()當(dāng)文件存在的時(shí)候就會(huì)清空原來的文件,覆蓋之前的文件
>>> handler = open('2.txt', 'wt')
>>> handler.write('111')
3
>>> handler.flush()
>>> handler.close()8.小練習(xí)
把一個(gè)字典列表中的value 拿出來寫入到一個(gè)文件里
#!/usr/bin/python
students = [
{'id' : 1, 'name' : 'likuan', 'sex' : 'm', 'age' : 25},
{'id' : 2, 'name' : 'lizhi', 'sex' : 'm', 'age' : 22},
]
path = 'student.txt'
handler = open(path, 'wt')
for student in students:
print(student)
print('{0},{1},{2},{3}'.format(student['id'],student['name'],student['sex'],student['age']))
handler.write('{0},{1},{2},{3}'.format(student['id'],student['name'],student['sex'],student['age']))
handler.write('\n')
handler.close()從寫入的文件在在轉(zhuǎn)換成一個(gè)字典列表
#!/usr/bin/python
path = 'student.txt'
students = []
handler = open(path, 'rt')
for line in handler:
line = line.strip()
id, name, sex, age = line.split(',')
students.append({'id':id,'name': name,'sex': sex,'age': age})
handler.close()
print(students)9.一些區(qū)別
x 和 w 的區(qū)別 w在文件存在會(huì)清空 文件的內(nèi)容 x 不會(huì)清空文件的內(nèi)容
>>> handler = open('2.txt', 'xt')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileExistsError: [Errno 17] File exists: '2.txt'a 是相當(dāng)于追加
>>> handler = open('2.txt', 'at')
>>> handler.write('test')
4
>>> handler.close()文件指針 tell()查看文件指針的位置,seek() 重置文件指針的位置
>>> handler = open('2.txt', 'rt')
>>> handler.read()
'111test'
>>> handler.tell()
7
>>> handler.read()
''
>>> handler.seek(0,0)
0
>>> handler.read()
'111test'小總結(jié) 關(guān)于文件mode
r+ 文件必須存在
w+ 文件如果存在清空
x+ 文件必須不存在
a+ 只往文件的末尾寫
10.小練習(xí)2定義一個(gè)模板文件
例如模板文件中的配置
<mysql>
<host>{HOST}</host>
<port>{PORT}</port>
<user>{USER}</user>
</mysql>
通過format傳值
#!/usr/bin/python tpl_file = 'tpl.xml' conf_file = 'mysql.xml' tpl = open(tpl_file).read() open(conf_file, 'wt').write(tpl.format(HOST='127.0.0.1',PORT='80',USER='kk'))
結(jié)果如下:
[root@Devop-python 19-1-20]# cat mysql.xml <mysql> <host>127.0.0.1</host> <port>80</port> <user>kk</user> </mysql>
網(wǎng)頁(yè)標(biāo)題:Python自動(dòng)化運(yùn)維開發(fā)----基礎(chǔ)(十一)文件操作
URL網(wǎng)址:http://chinadenli.net/article24/goiece.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站制作、網(wǎng)站導(dǎo)航、服務(wù)器托管、品牌網(wǎng)站設(shè)計(jì)、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)