Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the file operation methods of python?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what are the file operation methods of python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Opening and closing of files

Use try-finally mode

Try: F = open ('/ path/to/file', mode='r') print (f.read ()) finally: f.close ()

Use the with method, which is strongly recommended!

With open ('/ path/to/file', mode='r') as f: print (f.read ())

Supplementary content:

With with mode, you can open multiple files with one sentence at the same time. For a specific example, please see "File Modification".

Common parameters of the open () method:

The first parameter is the file to be opened. Only the file name can be written in the current path, and the full path name can be written in the file name that is not in the current path.

The second parameter, encoding, specifies the file decoding method. For a case, please see "File Modification".

The third parameter, mode, specifies the mode of operation of the file.

Mode= *) read mode of the file (error will be reported if the file does not exist, and the file will be read if it exists. File pointer at the beginning)

R, type text files in read-only mode. This is the default mode.

Rb, read-only to open binary files (such as pictures, sounds, videos).

Read / write to open the text file.

Open binary files in rb+, read-write mode.

Write mode (if the file does not exist, it will be created; the existence of the file will be overwritten. File pointer at the beginning)

W, open the text file in write-only mode.

Wb, open binaries in write-only mode.

Read and write to open the text file.

Open binary files in wb+, read-write mode.

Append mode (if the file does not exist, it will be created; if the file exists, it will be appended. File pointer at the end)

A, type text documents only.

Ab, write-only type binary files.

A text file is written and read.

Ab+, reads and writes binary files.

Reading and writing of documents

The read () method, which reads a binary or text file.

Read (n), the parameter n specifies how many bytes to read, and no parameter means to read all the contents.

The readline () method reads one line of the text file to a newline character (including newline characters).

The readlines () method reads all the contents of the text file to the list, and one line corresponds to a list element (including newline characters).

The for loop reads the text file. Large files are strongly recommended in this way! File handles are also objects that can be read line by line with a for loop, and can be read line by line with "yield" and returned line by line for processing.

With open ('a.txtforth, encoding='utf8') as f: for line in f: print (line,end='')

The write () method writes the contents of the file

Text files are written using f.write (str)

Binary files are written using f.write (bounded bytes')

When writing, it is a temporary buffer, it is not actually written to disk, and it is written to disk only when the file is closed. Alternatively, the contents of the buffer can be sent to disk immediately through the f.flush () method.

Pointer operation of file

Tell () # gets the cursor position in bytes

Seek () # adjusts the cursor position in bytes

Seek (0,0) # move to the header seek (0,2) # move to the modification of the tail file

Open the original file in read-only mode

Create a new file in write-only mode (usually named as the original file followed by .bak)

Read the contents of the original file into memory, modify the contents and save them to the new file.

Delete the original file.

Rename the new file to the original file.

The code example is as follows:

With open ('original .txt', encoding='utf8') as F1,\ open ('original .txt.bak', encoding='utf8', mode='w') as f2: for txt in F1: new_content = txt.replace ('python',' PYTHON') f2.write (new_content) os.remove ('original .txt') os.rename ('original .txt.bak' This is the end of the introduction of "what are the file operation methods of python?" Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report