Python: File operations

Source: Internet
Author: User
Tags contact form

Python: File operations

Open () calls the built-in functions, built-in function calls within the system's internal open, all operations on the file are based on the file handle F1.

Steps for file operations:

Close file, open file, action file

Remember: Finally close the file (otherwise there may be unexpected results)

Open File

File handle = open (' File path ', encoding= ' Specify file Encoding ', mode= ' mode ')

F1 = Open (r'd:/Contact form. txt', encoding='GBK', mode='R') #指定绝对路径Print(F1.read ()) F1.close () F1= Open ('Log1', encoding='GBK', mode='R') #相对路径是在该py文件同级目录下Print(F1.read ()) F1.close ()

To prevent forgetting to close a file, you can use the context Manager to open the file

With open (' File path ', ' mode ') as file handle:

With open ('Log1', encoding='Utf-8') as F1, open ('log2', encoding='Utf-8', mode='W') as F2:content=F1.read () f2.write (content) with open ('Log1', encoding='Utf-8') as F1:Print(F1.read ()) F1.close ()PassWith Open ('Log1', encoding='Utf-8', mode='W') as F2:f2.write ('666')

The mode of opening the file is:

R, read-only mode (default). W, write-only mode. "Non-readable; does not exist; create; Delete content;" A, append mode. "readable; not present; create; append only;" R+, can read and write files. "readable, Writable, append" W +, write "U" means that when reading, \ r \ n \ r \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ r \ r+ mode is used RUr+ U " b " represents the processing of binary files (such as: FTP send upload ISO image file, Linux can be ignored, Windows processing binary files need to be labeled) Rbwbab

Close File

File handle. Close ()

File: Read
#读 R
#1 Read () All outF1 = open ('Log1', encoding='Utf-8') Content=F1.read ()Print(content) F1.close ()#2 read (n) reading partF1 = open ('Log1', encoding='Utf-8') Content= F1.read (3)Print(content) F1.close () F1= Open ('Log1', mode='RB')Print(F1.read (3). Decode ('Utf-8') ) F1.close ()#R-Mode read (n) n reads by character. #RB mode read (n) n reads in bytes. #3 ReadLine () read by lineF1 = open ('Log1', encoding='Utf-8')Print(F1.readline ())Print(F1.readline ())Print(F1.readline ()) F1.close ()#4 readlines () reads each line sequentially F1 = open ('Log1', encoding='Utf-8')Print(F1.readlines ()) F1.close ()#5 for LoopF1 = open ('Log1', encoding='Utf-8') forLineinchF1:Print(line) f1.close ()

# r+  Read -Write F1 = open ('log1', encoding='utf-8', Mode='r+')print(F1.read ())f1.write (' 666 ') f1.write ('a')print(F1.read ()) F1.close () 

File: Write
#w No files, new file write content#with the original file, the content is first emptied, and the new content is written. F1 = open ('log2', encoding='Utf-8', mode='W') F1.write ('Peach in vain FDKSAGDFSA') F1.close ()#Reading and writing of imagesF1 = open ('1.jpg', mode='RB') Content=f1.read () F2= Open ('2.jpg', mode='WB') f2.write (content) F1.close () f2.close ()#w+ First Write and then readF1 = open ('log2', encoding='Utf-8', mode='w+') F1.write ('two engines.') F1.seek (0)Print(F1.read ()) F1.close ()#Append a#a no file, new file write contentF1 = open ('Log3', encoding='Utf-8', mode='a')#F1.write (' Alex 666 ')F1.write ('\nalex 666') F1.close ()#A +F1 = open ('Log3', encoding='Utf-8', mode='A +') F1.write ('Python22 Period') F1.seek (0)Print(F1.read ()) F1.close ()

Files: changing

The procedure is as follows:

# 1, open the original file in read mode.
# 2, open a new file in write mode.
# 3, read the original file as required modify to write the modified content to the new file.
# 4, delete the original file.
# 5, rename the new file to the original file.

ImportOswith Open ('file', encoding='Utf-8') as F1, open ('File.bak', encoding='Utf-8', mode='W') as F2:old_content=f1.read () new_content= Old_content.replace ('Alex','SB') F2.write (new_content) os.remove ('file') Os.rename ('File.bak','file')

Upgrade version
ImportOswith Open ('file', encoding='Utf-8') as F1, open ('File.bak', encoding='Utf-8', mode='W') as F2: forLineinchF1:new_line= Line.replace ('SB','Alex') F2.write (new_line) os.remove ('file') Os.rename ('File.bak','file')

Other operations:
#whether the readable is readable#whether writable can be written#F1.seek (#任意调整)#F1.seek (0,2) #光标调整到最后#f1.seek (0) #光标调整到开头#F1.tell () #告诉光标的位置#f1.truncate (3) #按照字节对原文件进行截取 must be in a or a + modeF1= Open ('Log3', encoding='Utf-8', mode='A +')#f1.write (' python22 period ')#print (F1.read ())Print(F1.readable ())Print(F1.writable ()) F1.close () F1= Open ('log2', encoding='Utf-8') F1.read ()Print(F1.tell ())Print(F1.seek (0))Print(F1.seek (0,2)) F1.seek (02H#arbitrary AdjustmentF1.seek (0,2)#the cursor adjusts to the lastF1.seek (0)#the cursor adjusts to the beginningPrint(F1.tell ())#tell the cursor wheref1.close () F1= Open ('Log3', encoding='Utf-8', mode='A +') F1.truncate (3)#interception of the original file according to Byte must be in a or a + modeF1.close ()

Python: File operations

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.