File manipulation in Python the Open () function

Source: Internet
Author: User

File operation:

1. About open mode:

R opens in read mode, F=open (r "C:\Users\shaopeng\Desktop\py_homework\DAY6\readme.txt", "R")

W is opened in writing, and if the file exists, it will clean up the contents of the original file and write it into the new file.

F=open (R "C:\Users\shaopeng\Desktop\py_homework\DAY6\readme.txt", "W") #那么原来readme the contents of the. txt file will be cleaned out

F.wirte ("I am very good") #那么文件的原内容就会被清洗掉, now Readme.txt content is only I am very good this sentence

a opens in Append mode (starting with EOF and creating a new file if necessary)

>>>f=open (R"C:\Users\shaopeng\Desktop\py_homework\DAY6\readme1.txt","  a")>>> f.write ("I'll try my best to beyond myself"        This is the total number of bytes in the py3.5.1 that will return the written sentence

R+ Open in read-write mode

1F=open (R"C:\Users\shaopeng\Desktop\py_homework\DAY6\readme1.txt","r+", encoding='Utf-8')2R=f.tell ()#returns 0, indicating that the pointer begins at the beginning of the article3 Print(R)4 5Content=F.read ()6 Print(content)7 #I want to be very execellent8 St to Beyond Myselfi want to be very execellent9 Ok,i want to be very execellentTen The above is the entire contents of the Read file OneR=f.tell ()#returns 112, which is at the end of the file, because after read, the pointer automatically jumps to the last A Print(R) -content1=F.readline () - #return 112, the pointer is already at the end, read backwards no content content1 is empty the Print(CONTENT1) -R=F.tell () - Print(R) -W=f.write ("Ok,i want to be very execellent"+"\ n") +r1=F.tell () - Print(R1)#returns 145, which is the length of the string after writing ok,i want to being very execellent and 33 more bytes .
View Code

w+ Open in read/write mode (see W)

1F=open (R"C:\Users\shaopeng\Desktop\py_homework\DAY6\readme1.txt","w+", encoding='Utf-8')2R=F.tell ()3 Print(r)#return 0, at the very beginning of the article4Content=F.read ()5 Print(content)6R=F.tell ()7 Print(r)#return 0,w+ mode open, the contents of the file is emptied, so actually read does not have any content8content1=F.readline ()9 Print(CONTENT1)TenR=F.tell () One Print(r)#just like the above . AW=f.write ("Ok,i want to be very execellent"+"\ n") -r1=F.tell () - Print(R1)#returns 33, which is the byte length of the last written string
View Code

A + opens in read/write mode (see a)

1F=open (R"C:\Users\shaopeng\Desktop\py_homework\DAY6\readme1.txt","A +", encoding='Utf-8')2R=F.tell ()3 Print(r)#return 33, open with A +, the pointer jumps to the end of the file, the original length of the file is 33, so return to4Content=F.read ()5 Print(content)6R=f.tell ()#return 33, opened in a A +, the pointer jumps to the end of the file, the original length of the file is, the Read method after reading the pointer jumps to the last, so return to7 8W=f.write ("Ok,i want to be very execellent"+"\ n")9r1=F.tell ()Ten Print(R1)#returns 66, after the string is written, the pointer is changed from the original 33 to 33+33, which is the
View Code

RB opens in binary read mode
WB opens in binary write mode (see W)
AB opens in binary append mode (see a)
Rb+ opens in binary read/write mode (see r+)
Wb+ opens in binary read/write mode (see w+)
Ab+ opens in binary read/write mode (see A +)

2, Withopen mode, with open (file path, mode, encoding format) as file name: This method at last do not close the file themselves, Python will help you automatically close the file, in this mode can open two files at the same time a read, a write in

>>> with open (r "C:\Users\shaopeng\Desktop\py_homework\DAY6\readme.txt", "R") as F1,open (r "C:\Users\ Shaopeng\desktop\py_homework\day6\readme1.txt "," W ") as F2:
For line in F1:
F2.write (line)

After this code operation, my corresponding folder has a readme1.txt file, the content is identical to Readme.txt

3, some methods of operation in the file

f.read ([size]) #size为读取的长度, in bytes

f.readline ([size]) #读一行, if size is defined, it is possible to return only part of a row

f.readlines ([size]) #把文件每一行作为一个list的一个成员 and returns the list. In fact, its internal is through the Loop call ReadLine () to achieve. If you provide a size parameter, size is the total length of the read content, which means that it may be read only to a portion of the file.

f.write (str) #把str写到文件中, write () does not add a newline character after Str

f.writelines (seq) #把seq的内容全部写到文件中 (multi-line write-once). This function simply writes faithfully and does not add anything after each line, including newline characters.

f.close () #关闭文件.  Python will automatically close files after a file is not used, but this feature is not guaranteed and it is best to develop a habit of shutting them down. If a file is closed and then manipulated, it generates VALUEERROR

F.flush () #把缓冲区的内容写入硬盘

F.fileno () #返回一个长整型的 "file label"

F.isatty () #文件是否是一个终端设备文件 (on UNIX systems)

F.tell () #返回文件操作标记的当前位置, starting with the origin of the file

f.next () #返回下一行 and shifts the file action marker to the next line. When a file is used for a statement such as for ... in file, it is called the next () function to implement the traversal.

F.seek (offset[,whence]) #将文件打操作标记移到offset的位置. This offset is generally calculated relative to the beginning of the file and is generally a positive number. However, if the whence parameter is provided, whence can be calculated from scratch for 0, and 1 for the current position as its origin. 2 means that the end of the file is calculated as the origin. Note that if the file is opened in a or a + mode, the file action tag is automatically returned to the end of the file each time the write operation is made.

f.truncate ([size]) #把文件裁成规定的大小, the default is the location that is cropped to the current file action tag. If the size of the file is larger, depending on the system may not change the file, it may be 0 files to the corresponding size, it may be some random content to add.

File manipulation in Python the Open () function

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.