day-2 python 文件讀寫模式r,r+,w,w+,a,a+的區別


r+和w+都是可讀可寫,區別在於r+讀文件時,不會覆蓋之前的內容,之前的內容能夠讀出來;w+讀時,會覆蓋之前的內容;所以讀文件時,用r或者r+

#讀操作 r

1 filepath = 'aa.log' #aa.log存在
2 #讀操作
3 #read()方法,一次都讀出來
4 with open(filepath,'r') as f:
5     print(f.read())
C:\Users\admin\Miniconda3\python.exe E:/python/文件讀寫.py
hello world!


Process finished with exit code 0
1 filepath = 'a.log'  #a.log不存在,執行報錯
2 #讀操作
3 #read()方法,一次都讀出來
4 with open(filepath,'r') as f:
5     print(f.read())
C:\Users\admin\Miniconda3\python.exe E:/python/文件讀寫.py
Traceback (most recent call last):
  File "E:/python/文件讀寫.py", line 4, in <module>
    with open(filepath,'r') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'a.log'

Process finished with exit code 1

 

#讀寫操作 r+

1 #r+寫操作
2 filepath = 'aa.log'
3 with open(filepath, 'r+') as f:
4     f.write('文字被替換成hahaha')
5     print(f.read())
C:\Users\admin\Miniconda3\python.exe E:/python/文件讀寫.py
被替換成hahaha

Process finished with exit code 0

#readline()讀操作

1 filepath = 'my.log'
2 with open(filepath,'r',encoding='UTF-8') as f:
3     print(f.readline().strip())
4     print(f.readline().strip())
C:\Users\admin\Miniconda3\python.exe E:/python/文件讀寫.py
2019-10-12 09:34:23,073 - E:/python/test.py[line:9] - DEBUG: 默認日志級別是debug
2019-10-12 09:34:23,075 - E:/python/test.py[line:10] - INFO: info級別

Process finished with exit code 0

#readlines()讀操作,讀到的內容,返回一個列表

1 #readlines()方法,一次都讀出來,並返回一個列表
2 with open(filepath,'r',encoding='UTF-8') as f:
3     print(f.readlines())
C:\Users\admin\Miniconda3\python.exe E:/python/文件讀寫.py
['2019-10-12 默認日志級別是debug\n', '2019-10-12 info級別\n', '\n']

Process finished with exit code 0
1 #將readlines讀到的內容存放到line中,使用for循環遍歷內容
2 with open(filepath,'r',encoding='UTF-8') as f:
3     lines = f.readlines()
4 for line in lines:
5     print(line.strip())
C:\Users\admin\Miniconda3\python.exe E:/python/文件讀寫.py
2019-10-12 默認日志級別是debug
2019-10-12 info級別


Process finished with exit code 0

 

1 # 如果要逐行讀取,直接遍歷文件對象就可以了
2 with open(filepath,'r',encoding='UTF-8') as f:
3     for line in f:
4         print(line.strip())
C:\Users\admin\Miniconda3\python.exe E:/python/文件讀寫.py
2019-10-12 默認日志級別是debug
2019-10-12 info級別


Process finished with exit code 0

 

1 #寫操作
2 with open(filepath,'w',encoding='UTF-8') as f:
3     f.write('hello world!\n')
4 
5 with open(filepath,'r',encoding='UTF-8') as f:
6     print(f.read())
C:\Users\admin\Miniconda3\python.exe E:/python/文件讀寫.py
hello world!


Process finished with exit code 0

#w+讀寫模式

1 with open(filepath,'w+',encoding='UTF-8') as f:
2     f.write('hello world!\n')
3     f.seek(0)
4     print(f.read())
C:\Users\admin\Miniconda3\python.exe E:/python/文件讀寫.py
hello world!


Process finished with exit code 0

#a+追加讀寫

1 with open(filepath,'a+',encoding='UTF-8') as f:
2     f.write('hello world!\n')
3     f.seek(0)
4     print(f.read())
C:\Users\admin\Miniconda3\python.exe E:/python/文件讀寫.py
hello world!
hello world!


Process finished with exit code 0

 

 1 #讀模式,只能讀,不能寫,默認讀模式
 2 f = open(r'E:\cnz\day1\列表.py','r',encoding='utf-8')
 3 result = f.read()
 4 print(result)
 5 f.close()
 6 
 7 #讀模式r 寫模式w 追加模式a
 8 #寫模式,只能寫,不能讀,寫模式會覆蓋之前的內容
 9 
10 f = open('test.txt','w',encoding='utf-8')
11 f.write('abc')
12 f.close()
13 
14 # 追加模式,寫文件時,不會覆蓋之前的內容
15 f = open('test.txt','a',encoding='utf-8')
16 # f.write('哈哈哈')
17 f.read()
18 f.close()
19 
20 # 總結:
21 # 讀模式,只能讀,不能寫,打開不存在的文件會報錯
22 # 寫模式,只能寫,不能讀,打開不存在的文件會創建
23 # 追加模式,只能寫,不能讀,在原文件基礎上增加內容,打開不存在的文件,會創建
24 # 只要跟r有關的,打開不存在的文件,會報錯
25 # 只要和w有關,都會清空之前的文件
26 # a+模式,文件指針模式是在末尾的,如果想讀文件,需要seek(0)
27 #writelines傳一個list,會把list里面的元素寫到文件里
28 
29 # r+       w+       a+
30 # 讀寫模式  寫讀模式  追加讀模式
31 
32 f = open('test3.txt','a+',encoding='utf-8')
33 f.write('哈哈哈1')
34 f.read()
35 f.close()
36 
37 l = ['a','b','c','d']
38 f = open('test.txt','w',encoding='utf-8')
39 f.writelines(l)
40 f.close()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM