python3.x file Operations

Source: Internet
Author: User


The mode of opening the file is:
1. Read-only mode (default)
2. Write-only mode (unreadable, not present, created, overwrite if present)
3. Append mode (readable, non-existent, create, only append content if present)

"+" means that you can read and write to a file:
1. r+ can read and write files (readable, writable, can be appended)
2. w+ Write Read
3. A + Append

"U" means that the \ r \ n \ r \ \ \ \ \ \ \ \ \ \ \ \ \ r/N (r+ mode) can be converted automatically when reading
Because the line break for the Windows system is \ n for the \r\n,linux system, plus u can automatically convert \ r \ n
1. RU
2. R+u

"B" means processing binary files
1.rb
2.wb
3.ab



#Coding:utf-8#opening File open ()f = open ('Test.txt','r+')#or with open () after this method operation is completed, the close () does not need to be closed automaticallyWith open ('Test.txt','R') as F:f.read ()#Close Filef = open ('Test.txt','r+', encoding='Utf-8') ret=F.read ()Print(ret) f.close ()#reads the contents of the file (you can specify each character read)f = open ('Test.txt','r+', encoding='Utf-8') ret= F.read (8)Print(ret)#read data (can specify number of characters to read), Save as List displayf = open ('Test.txt','r+', encoding='Utf-8') ret=F.readlines ()Print(ret) f.close ()#reading a row of dataf = open ('Test.txt','r+', encoding='Utf-8') ret=F.readline ()Print(ret) f.close ()#Write file The write () parameter is a stringf = open ('Test.txt','A +', encoding='Utf-8') F.write ("ABC") ret=F.read ()Print(ret) f.close ()#write to file, Writelines () parameter is a sequence, such as a list, it will iterate to help you write to the filef = open ('Test.txt','A +', encoding='Utf-8') F.writelines (["AA","BB","cc"]) ret=F.read ()Print(ret) f.close ()#determine if the file is a unified TTY devicef = open ('Test.txt','r+', encoding='Utf-8') ret=F.isatty ()Print(ret)#Falsef.close ()#determine if it is readable (non-readable, "No such file or directory:")f = open ('Test.txt','r+', encoding='Utf-8') ret=f.readable ()Print(ret)#Truef.close ()#specify the position of the pointer in the filef = open ('Test.txt','r+', encoding='Utf-8') ret= F.read (8)#Read 8 characters FirstPrint(ret) f.seek (0)#then move the pointer to the beginning of the fileret = F.read (8)#after re-readingPrint(ret) f.close ()#Get pointer positionf = open ('Test.txt','r+', encoding='Utf-8') ret= F.read (8)#Read 8 characters FirstPrint("Pointer position:%s"%f.tell ())#view current pointer positionPrint(ret) f.seek (0)#Reset specified to start bitPrint("Pointer position:%s"%f.tell ())#when viewing the pointer positionf.close ()#truncate file data, preserving only specified previous data (specified number of bytes)f = open ('Test.txt','r+', encoding='Utf-8') F.truncate (8)#The file retains only the first 8 bytes of data, and all the data behind the file is deleted.RET =F.read ()Print(ret) f.close ()#File DescriptorF.fileno ()#flush File Internal buffersF.flush ()

python3.x 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.