Python file operations
Normal file operations are divided into three steps:
Open File
Manipulating files
Close File
Syntax: File handle = open (file name, mode)
R, read-only mode (default).
W, write-only mode. "unreadable; not exist; create; delete content;"
A, append mode. "Readable; not exist" create; "only append content;"
"+" means you can read and write a file at the same time
"U" means that when reading, \ r \ n \ r \ \ \ \ \ \ \ \ \ \ n (note: can only be used with R or r+ mode)
"B" means processing binary files (e.g. FTP send upload ISO image file, Linux can be ignored, Windows process binary file should be labeled)
r+, can read and write files. "readable; writable; can be added"
w+, write and read first, use with Seek
A +, with a
Several common methods
Read () #读取文件所有内容并返回字符串
ReadLine () #读取一行
Next () #读取下一行
ReadLines () #读取所有内容 and returns the list (an element value of a behavior list)
Write () #写一行
Writelines (list) #写多行, parameter is List
Seek () #句柄指针操作
Tell () #当前句柄指针位置
Truncate () #截取文件句柄头到当前位置的字符串, return None
Xreadlines () # One line reads, 3.x version has discarded this notation
Sample code
#code Example#open a file and writeFH = open ('Tfile','wb+')#where the file handle is locatedFh.tell ()#Write a rowFh.write ('Life was like a roller coaster,live it,be happy,enjoy life.\n')#write multiple lines, parameter is listFh.writelines (["The best your dreams come true was to wake up.\n","If you're not making mistakes,you ' re isn't trying hard enough."])#returns the file handle headerfh.seek (0)#read a lineFh.readline ()#read all rows, return listFh.readlines ()#returns the file handle headerfh.seek (0)#reads the entire content, returns the stringFh.read ()#returns the file handle headerfh.seek (0) fh.readline ()#Read current positionFh.tell ()#A string that intercepts the header of a file handle to its current positionfh.truncate () fh.seek (0) Fh.read () fh.writelines (["The best your dreams come true was to wake up.\n","If you're not making mistakes,you ' re isn't trying hard enough."])#read a linefh.seek (0) fh.next ()#Close file handleFh.close ()
Result
1>>>#code Example2...#open a file and write3... fh = open ('Tfile','wb+')4>>>#where the file handle is located5 ... Fh.tell ()6 07>>>#Write a row8... fh.write ('Life was like a roller coaster,live it,be happy,enjoy life.\n')9>>>#write multiple lines, parameter is listTen... fh.writelines (["The best your dreams come true was to wake up.\n","If you're not making mistakes,you ' re isn't trying hard enough."]) One>>>#returns the file handle header A ... fh.seek (0) ->>>#read a line - ... fh.readline () the 'Life was like a roller coaster,live it,be happy,enjoy life.\n' ->>>#read all rows, return list - ... fh.readlines () -['The best your dreams come true was to wake up.\n',"If you're not making mistakes,you ' re isn't trying hard enough."] +>>>#returns the file handle header - ... fh.seek (0) +>>>#reads the entire content, returns the string A ... fh.read () at "Life was like a roller coaster,live it,be happy,enjoy life.\nthe best-of-Do your dreams come true is to wake up.\ NIf you ' re not making mistakes,you ' re isn't trying hard enough." ->>>#returns the file handle header - ... fh.seek (0) ->>>Fh.readline () - 'Life was like a roller coaster,live it,be happy,enjoy life.\n' ->>>#Read current position in ... Fh.tell () -59 to>>>#A string that intercepts the header of a file handle to its current position + ... fh.truncate () ->>>fh.seek (0) the>>>Fh.read () * 'Life was like a roller coaster,live it,be happy,enjoy life.\n' $>>>#read a linePanax Notoginseng ... fh.seek (0) ->>>Fh.next () the 'Life was like a roller coaster,live it,be happy,enjoy life.\n' +>>>#Close file handle A...
When the file is very large, to a line of read the file, to use for loop, the example is as follows
FH = open ('tfile') for in FH: Print line fh.close ()
Sometimes we often forget to close the file handle, if you do not want to write a closed file handle code, you can use with the context operation (support to operate multiple files simultaneously)
Grammar:
With open ('tfile1') as Fh1,open ('tfile2') as FH2: ....
Python file operations