Python file operations

Source: Internet
Author: User
Tags readline

function of file operation

Open ("FileName (path)", mode= "?", encoding= "character set")

Mode

R, RB Read only

W,WB Write only

A,ab Append

r+ Reading and writing

w+ Write Read

A + write read (append read)

Two read-only (R,RB)

1. Read-only (R,RB)

f = open ("Aaa.txt", mode= "R", encoding= "utf-8") content = F.read () print (content) F.close ()

RB read out is the bytes type, in RB mode, cannot choose the encoding character set

f = open ("Aaa.txt", mode= "RB") content = F.read () print (content) F.close ()

RB is used to read non-text files, such as MP3, pictures, videos, etc. because this data is not directly displayed. We will use it later when we upload and download the file. And the live broadcast we're looking at, it's actually this kind of data.

2. Absolute path and relative path:
1. Absolute path: Start from the root of the disk until the file name.
2. Relative path: A file under the same folder. Relative to the folder in which the program is currently located. If the same
In a folder. The relative path is the file name. If the folder is in the previous layer. Then you have to. /

Using relative paths is recommended

3. How to manipulate files

1.read ()

Read all the contents of the file, the disadvantage: the memory, if the file is too large, it is easy to cause memory crashes

  

f = open ("Bbb.txt", mode= "R", encoding= "utf-8") content = F.read () print (content)

2.read (N)

To read n characters, it is important to note that if you read it again, it will be read at the current location instead of reading from, if you are using RB mode. It reads n bytes out of the

  

f = open ("Bbb.txt", mode= "R" encoding= "Utf-8") content = F.read (4) print (content)

3.readline () reads one row of data at a time, note: ReadLine (), note that every time you read the data will have a \ n, you need to use the strip () method to remove \ n or space

f = open ("Ccc.txt", mode= "R", encoding= "Utf-8") Content1 = F.readline () Content2 = F.readline () Content3 = F.readline ()

  

4.readlines () creates an element for each row, puts it in a list, reads all the contents, and easily causes memory crashes, not recommended

  

f = open ("Ccc.txt", mode= "R", encoding= "utf-8") LST = F.readlines () print (LST) for line in LST:    print (Line.strip ())

5. Cyclic reading, this method is the best, every time a row of content, no memory overflow problem

f = open ("Ccc.txt", mode= "R", encoding= "Utf-8") for line in F:    print (Line.strip ())

Read the file handle must close F.close ()

Three, write mode (W,WB)

When writing, if there is no file, the file is created, and if the file exists, the original content is deleted and then written to the new content

f = open ("xxx", mode= "W", encoding= "Utf-8") f.write ("Asdf") F.flush () # Refresh F.close ()
Iv. Append (A,ab)

In append mode, the content we write is appended to the end of the file.

f = open ("zzz", mode= "a", encoding= "Utf-8") f.write ("Asdf") F.flush () F.close ()
V. Read and write mode (R+,R+B)

For read-write mode, must be read first, because the default cursor is at the beginning, ready to read, and then write after reading, we later use the most frequent mode is r+

f = open ("zzz", mode= "r+", encoding= "utf-8") content = F.read () f.write ("Asdf") print (content) F.flush () F.close ()
Vi. Writing and Reading (W+,W+B)

The contents are emptied, then written, and finally read, but the contents are empty, not long

f = open ("zzz", mode= "w+", encoding= "Utf-8") f.write ("asdf") content = F.read () print (content) F.flush () F.close ()
Vii. Additional Reading (A +)

In a + mode, the data is not read either first or post-read.

f = open ("zzz", mode= "A +", encoding= "Utf-8") f.write ("asdf") content = F.read () print (content) F.flush () F.close ()
Viii. Other operations

1. Seek (n) move the cursor to n position, note that the unit of movement is byte

Move to the beginning: Seek (0)

Move to the end: Seek (0,2)

2. Tell () can help us get to where the current cursor is

3. Truncate () Truncate file

Ix. Modification of documents

File modification: Only the contents of the file can be read into memory, the information is modified, and then the source file is deleted, the new file name is changed to the source file name

Import Oswith Open ("Alex.txt", mode= "R", encoding= "Utf-8") as F1,    open ("alex_ copy. txt", mode= "W", encoding= "Utf-8") As F2:    for line in F1: line        = Line.replace ("Alex", "SB")        F2.write (line) os.remove ("Alex.txt") Os.rename (" Alex_ copy. txt "," alex.txt ")  

  

  

  

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.