Python---file operations

Source: Internet
Author: User

first, the document operation process
1. Open the file, get the file handle and assign a value to a variable
2. Manipulating files with file handles
3. Close the file


Second, the file open mode
F = open ("File_test", "R",encoding =  "Utf-8")      #打开文件, "read mode", read Only, Get the file handle and assign a value to a variable print (F.read ())                        #读文件所有内容, after reading the file cursor jumps to the end, the file is used with caution f.close () f1 = open ("File _test "," W ", encoding=" Utf-8 ")       #写模式, creates a new File_test file and writes F1.write ("-------------") F1.close () F2 = open ("File_test", "a", encoding= "Utf-8")       #追加模式, appended at the end of the original file, New F2.write ("-------------") f2.close ("F3  = open") ("File_test", "r+", encoding= "Utf-8") without the original file       #读写模式, written at the end of the original file content appended, no original file is new print (F3.readline ())                     #按行读print (F3.readline ()) print (F3.readline ())                   # Print the first three lines, this time the cursor moves to the third row position print (f3.tell ())                       #打印光标位置f3. Write ("--------------")                  #但是写入还是文件内容最后写入f3. Close () F4  = open ("File_test", "w+", encoding= "Utf-8")      #写读模式, as long as "write in front" will build a new file in writing f4.write ("----------------------\ n") F4.write ("----------------------\ n") f4.write ("----------------------\ n") F4.seek (Ten)                           #光标移动到10的位置f4. Write ("test4")                       #再写入会将原内容覆盖f4. Seek (0)                          # Move the cursor to the beginning of the position print (F4.read ()) F4.close () #输出 #----------ni hao -----#----------------------#----------------------F5  = open ("File_ Test "," A + ", encoding=" Utf-8 ")      #追加读模式, appended at the end of the original file, without the original file New F5.write ("----------------------\ n ") f5.write ("----------------------\ n ") f5.write ("----------------------\ n ") F5.seek (Ten)                           #光标移动到10的位置f5. Write ("Test5")                       #再写入会在文件内容最后写入f5. Seek (0) print (F5.read ()) F5.close () f6   = open ("File_test", "RB")                    #以二进制文件格式读这个文件print (F6.readline ()) print (F6.readline ()) print (F6.readline ()) F6.close () F7  = open ("File_test", "AB")                    #以二进制文件格式追加这个文件f7. Write ("-------------------\ n". Encode ())          #encode   Converts the str character to Bytesf7.write ("-------------------\ n". Encode ()) f7.write ("-------------------\ n". Encode ()) F7.close () F8  = open ("File_test", "WB")                    #以二进制文件格式写这个文件f8. Write ("-------------------\ n". Encode ())         #encode   Converts the str character to Bytesf8.write ("-------------------\ n". Encode () ) f8.write ("-------------------\ n". Encode ()) F8.close () #注: Also Ru or r+u mode, "U" means that when reading, the  \r \n \r\ can be n Automatic conversion to  \n  (in conjunction with  r  or  r+  mode)

Iii. Circulation of documents

#按行循环 and replace line fifth with F = open ("File_test", "R", encoding= "Utf-8") Count = 0for lines in F:count + = 1 if count = = 5:pri NT ("----Split line----") Continue print (Line.strip ()) #strip是去除行首行尾的空格符和换行符f. Close () #f. Readli NES () #切记用f. ReadLines is to convert the file to a list first, if the file is too large to consume too much memory

Iv. Modification of documents

#打开一个文件, modified to write to a new file F = open ("File_test", "R", encoding= "Utf-8") f_new = open ("File_new", "W", encoding= "Utf-8") for line In F: #按行取出, each line is a string of strings if "Fengxiaoli" in line:line = Line.replace ("Fengxiaoli", "F Engxiaoli ") #对字符串进行操作, J f_new.write (line) F.close () F_new.close ()

Five, flush method

f = open ("File_test", "W", encoding= "Utf-8") f.write ("hello\n") f.write ("hello\n") f.write ("hello\n") F.flush () # When writing to a file, there is a cache that reaches a time and writes to the file at once. Flush Flushing executes immediately if the power outage may not have been successfully written

Vi. with statements

#with语句作用, in order to avoid opening the file after forgetting to close with open ("File_test", "R", encoding= "Utf-8") as f: #类似于f = open ("File_test", "R", encoding= "u Tf-8 ") with open (" File_test "," R ", encoding=" Utf-8 ") as F, \ #还可以同时打开多个文件 open (" File_test2 "," R ", encoding=" Utf-8 ") as F2:

Vii. Other operations

F = open ("File_test", "R", encoding= "Utf-8") print (F.tell ())                 #打印光标位置, Count print by character (F.readline ())             #按行读print (F.read)               #按字符读print (F.tell ()) f.seek (0)                      #把光标回到开头f. Seek ()                     #把光标移动到12个字符的位置print (F.readline ()) Print ( f.encoding)             #打印文件编码print (F.isatty ())              #判断文件是否是终端设备, return Ture or falseprint ( F.seekable ())           #判断是否能移动文件光标, return ture or falseprint (F.readable ())    &Nbsp;      #判断文件是否可读print (f.writable ())            #判断文件是否可写 # f  = open ("File_test", "a", encoding= "Utf-8") # f.truncate ()         #从头开始截取多少字符



Python---file operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.