Here is a small series for you to bring a Python file and stream (the example explained). Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
1. File Write
#打开文件, the path error will be incorrect F = open (R "C:\Users\jm\Desktop\pyfile.txt", "W") F.write ("hello,world!\n") F.close ()
2. File read
#读取f = open (R "C:\Users\jm\Desktop\pyfile.txt", "R") print (F.read ()) f.close () Output: hello,world!
3. Read and Write lines
ReadLine ([size]) reads the entire line, including the "\ n" character.
ReadLines ([sizehint]) reads all rows and returns a list, and if given sizeint>0, returns a row with a sum of approximately sizeint bytes, the actual read value may be larger than sizhint because the buffer needs to be populated.
Writelines (Sequence) writes a list of sequence strings to the file and adds a newline character to each line if a line break is required.
4. File objects are iterative and can be used directly in a for loop.