1.文件的路徑
(1) 相對路徑:文件所在路徑
(2) 絕對路徑:從根路徑起的目錄
2.文件的類型
(1)文本文件 : 可以直接編寫的文件
(2)二進制文件: 安裝包 ,圖片
3.mode相關
(1)關于文件操作的mode
mode 打開文件的方式
r 讀
w 寫
x 創建并寫
a 追加
r+ 讀寫
w+ 寫讀
x+ 創建并寫讀
a+ 追加讀
(2)關于文件的mode
mode 文本類型
t 文本(默認)
b 二進制
4.文件打開、讀取、關閉的過程
文件打開
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'
文件關閉
>>> handler.close()
5.讀文件
handler.read() 讀取所有的內容
handler.read(size) 讀取文件size個字節內容
handler.readline() 讀取文件一行
handler.readlines() 讀取文件所有行
判斷文件是否讀完的依據是判斷最后一行是否為空
文件的可遍歷性
>>> handler = open('tpl.xml', 'rt')
>>> for lines in handler:
... print(lines)
...
<mysql>
<host>{HOST}</host>
<port>{PORT}</port>
<user>{USER}</user>
</mysql>
>>> handler.close()對于二進制文件的讀方式 只有 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.字符串和二進制字符串的轉換
字符串轉換成二進制字符串
>>> "abc".encode() b'abc'
二進制字符串轉換成字符串
>>> b'abc'.decode() 'abc'
7.寫文件
寫文件 (文件不存在的時候創建)
>>> handler = open('2.txt', 'wt')
>>> handler.write('123')
3
>>> handler.close()當文件存在的時候就會清空原來的文件,覆蓋之前的文件
>>> handler = open('2.txt', 'wt')
>>> handler.write('111')
3
>>> handler.flush()
>>> handler.close()8.小練習
把一個字典列表中的value 拿出來寫入到一個文件里
#!/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()從寫入的文件在在轉換成一個字典列表
#!/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.一些區別
x 和 w 的區別 w在文件存在會清空 文件的內容 x 不會清空文件的內容
>>> handler = open('2.txt', 'xt')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileExistsError: [Errno 17] File exists: '2.txt'a 是相當于追加
>>> 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'小總結 關于文件mode
r+ 文件必須存在
w+ 文件如果存在清空
x+ 文件必須不存在
a+ 只往文件的末尾寫
10.小練習2定義一個模板文件
例如模板文件中的配置
<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'))
結果如下:
[root@Devop-python 19-1-20]# cat mysql.xml <mysql> <host>127.0.0.1</host> <port>80</port> <user>kk</user> </mysql>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。