Python---file operations

Source: Internet
Author: User

One, file-save data permanently

Applications that we write in Python or other languages need to be saved on the hard drive if we want to keep the data permanently, which involves the application operating hardware, and it is well known that the application is not able to manipulate the hardware directly. The operating system encapsulates complex hardware operations into a simple interface for use by the user/application, where the file is the operating system provided to the application to manipulate the virtual concept of the hard disk, and the user or application can save its own data permanently by manipulating the file.

Ii. Description of Python file operation

The following will be read and written from the file, as well as modify to describe how Python operation of the file, first introduced, Python file operation syntax.

f = open (' file path ' 'r', encoding=' Utf-8 ')
F.close ()

Commonly used syntax as above, first need to fill in the path to manipulate the file, and then select Mode, the commonly used mode has r (read), W (write), r+ (Read and write), w+ (read),

A (append write), and b mode (bytes mode) Read and write method

1. Read mode (R)

You need to create a file yourself before reading the schema, and then find the path and verify how the file is encoded to successfully read the file contents. The following is an example of creating text, file operations 1.txt, the path for the h:\ file operation 1.txt. The text reads as follows:

f = open ('h:\ file Operation 1.txt', mode='r', encoding='  GBK'= f.read ()  print(g)
F.close ()

As shown above, we choose R, read mode, encoding format because our operating system is GBK, so we set encoding to GBK

Operation Result:

The above described G.read (), is the file read method, read is the text of the string in a one-time reading to the memory and then assign to the variable, then everyone has not thought, if the text size has a few g or even more than 10 g, you load the content into memory once, then your computer memory will burst. So we usually take the lead one way. ReadLine () mode to read and write.

First we manually increase the content of the above text to three lines.

f = open ('h:\ file Operation 1.txt', mode='r', encoding='  GBK')print(F.readline ())
F.close ()
# Running Result: Congratulations successfully read the file Contents 1  = open ('h:\ file Operation 1.txt', mode='r', encoding='  gbk'printprint(F.readline ())
F.close ()
# running results Congratulations on successfully reading the contents of the file 1  = open ('h:\ file Operation 1.txt', mode='r', encoding='  gbk'print print print  (F.readline ())
F.close ()
# running results Congratulations on successfully reading the contents of the file 1

It can be found that every time we f.readline we read only the contents of each line in the text. In addition, the string can be iterated, that is, you can use for to follow the bad. The essence of the text is the stored string, so the contents of the file can also be broken iteration, and each time the bad element is the content of each line:

f = open ('h:\ file Operation 1.txt', mode='r', encoding='  GBK')  for in F: #line corresponds to the contents    of each row in F print (line)
F.close ()
# Operation Result: Congratulations on successfully reading the contents of the file 1 congratulations on successfully reading the contents of the file 2 congratulations successfully read the file content 3

Careful classmates can find that the print results are empty line between the text, because each line at the end of a ' \ n ' line break, print itself also has a newline character, so print out the result. So every time we load into the memory of the text content is a fixed line, there is no need to worry about memory problems.

Read.lines

The read.lines pattern reads each line of text as a list of elements in the list:

f = open ('h:\ file Operation 1.txt', mode='r', encoding='  GBK')print(F.readlines ())
F.close ()
# Operation Result: [' Congratulations on successfully reading the contents of the file 1\n' Congratulations on successfully reading the contentsof the file 2\n' Congratulations successfully read the contents of the file 3 \n '\ n']

You can see that the end of each element is \ n, as well as the reason why the previous use of ReadLine () is a blank line.

2, ' W ' read mode

W Mode: The file does not exist, it is created, it is deleted and then created. What do you mean, that is, when we choose W mode to open the file, if the file does not exist, then the system will follow the file path and filename you entered to automatically create the file, or delete the source file and then create the file.

f = open ('h:\ file Operation 2.txt', mode='w', encoding='  Utf-8') f.write (' Create text success ')
F.close ()

You can see that the text Operation 2 is not, is created by the W mode according to the path and file name you entered, F.write () is to add the content you want to write the text, the content can only be a string.

Now that text operation 2 already exists, what happens when we run the following program:

f = open ('h:\ file Operation 2.txt', mode='w', encoding='  Utf-8') f.write (' Create text and succeed ')
F.close ()

Operation Result:

You can see that when you create a file in W mode, if the original file exists, the original file is deleted and the file is created with the same name and the content is re-written.

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.