How do I read and write files in python?
Four parts:
Open File
Read file
Write file
Close File
Example:
How to open children files and show
Data=open ('children') #指向要打开的文件f=data.read () #读取文件内容 and assigned to Fprint (f) # Print file Data.close () #关闭文件, equivalent to returning memory
File open default is read-only permission, but we can open the file with additional permissions, such as Read and write
Data=open ('children','r+', encoding='utf-8 ') F=data.read ()print(f)print# View data Whether this file is readable Print (Data.writable ()) # View data whether this file is writable
Data.close ()
Operation Result:
Hello Word
True
True
Read the contents of a file row by line
Use for loop to read
Data=open ('children','r', encoding='utf-8 ') for in range (1,5): f=data.readline () Print (" prints line%d:" %i,f)
Operation Result:
Print line 1th: My name is children
Print Line 2nd: come from Beihai
Print line 3rd: like Bie
Print Line 4th: chenchaozhen
If you don't use a for loop, you can write only one line.
Data=open ('Children','R', encoding='Utf-8') F=Data.readline ()Print("First line", f) F=Data.readline ()Print("Second Line", f) F=Data.readline ()Print("Third line", f) F=Data.readline ()Print("Line Fourth", f) F=Data.readline ()Print("Line Fifth", F) data.close ()
Operation Result:
Print line 1th: My name is children
Print Line 2nd: come from Beihai
Print line 3rd: like Bie
Print Line 4th: Chen
Note that ReadLine reads a row, while ReadLines reads all and saves it as a list
Data=open ('children','r', encoding='utf-8 ')print#readlines is read all rows into a list form
Operation Result:
[' 1\n ', ' 2\n ', ' 2\n ', ' 3\n ', ' 4\n ', ' 5\n ']
Why would there be a \ n?
Because the Windows platform line break is \ n
How do I write the specified content to a file?
When the open file is given W permissions
Data=open ('children','w', encoding='utf-8 '# write mode data.write ("11112222"# Data.close ()
# #注意w权限是重新创建一个同名文件, then overwrite the source file
# #其实文件的修改可以理解为覆盖, overwrite old content with new content
Like ReadLines, Writelines is the same, but one is to read one is to write
Data=open ('Children','W', encoding='Utf-8') Data.writelines ("11112222\n")#Line-wise writeData.writelines ("11112222\n")#does not overwrite the previous line, which is equivalent to list writingData.writelines ("1223123") Data.close ()
You can also write this:
Data=open ('children','w', encoding='utf-8 ') data.writelines (['Chen Chao zhen \ n',' Xu Zhen zhi \ n'# So if you want to write more than a row, you can write it as a list data.close ()
#但是要注意, the list is inserted in the form of the last to add a \ n
#追加模式, the source file is not emptied and the content is appended to the last file
Data=open ('children','a', encoding='utf-8 '# Append mode data.writelines (['Chen Chao zhen \ n', ' Xu Zhen zhi \ n ' ]) data.close ()
The file has not been modified so that only covers
R Read mode
W Write mode (re-create an empty file with the same name)
A append mode (the Write content is appended to the last line of the file)
r+ Read-write mode (writes are overwritten from the beginning of the file, not empty files with the same name)
w+ Read-write mode (re-create an empty file with the same name)
A + append read and write mode (same as a mode)
1. Document A.txt content: Each line content is the product name, the price, the number, to find out the total amount of money spent on this shopping
Apple 10 3
Tesla 100000 1
MAC 3000 2
Lenovo 30000 3
Chicken 10 3
In the file open there is also a keyword with
With open ('children','r+') as name: Print (Name.readlines ())
Open file and create variable to point to file, can be completed in one step
With open ("a.txt",'r') as much: res=[] res1=[] for in much: res.append (line) for in range (0,5): a=int (Res[i].split () [1]) b=int (Res[i].split ( ) [2]) res1.append (a*b) print(sum (res1))
Operation Result:
196040
Python3 file read and write operations