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

Files and file systems of python

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Operation of files

In python, using the open function, you can open an existing file or create a new one.

Open (File name access Mode)

The file name / file path followed by open is usually a string.

The function of open is similar to the vim command (file exists-opens; file does not exist-new).

Access mode:

R: open the file read-only, and the pointer to the file is placed at the beginning of the file, which is the default mode

W: open a new file for writing only and overwrite it if it already exists. If the file does not exist, create a new file to write to

An Open a file for append. If the file already exists, the file pointer will be placed at the end of the file, that is, the new content will be written after the existing content, and if the file does not exist, the new file will be written

Rb opens a file in binary format for read-only use. The file pointer will be placed at the beginning of the file, which is the default mode.

Wb opens a file in binary format for writing only. Overwrite the file if it exists. If the file does not exist, create a new file to write to

Ab opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file, that is, the new content will be written after the existing content, and if the file does not exist, the new file will be written

R + equals r and writes data at the beginning of the file (that is, overwriting the data at the beginning of the file). This is also the difference between r + and r.

W + is equivalent to r and w. This is also the difference between w + and w.

A + is equivalent to r and a. This is also the difference between a + and a.

Rb+ opens a file in binary format for reading and writing. The file pointer will be placed at the beginning of the file

Wb+ opens a file in binary format for reading and writing. If the file already exists, overwrite it. If the file does not exist, create the file

Ab+ opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a file for reading and writing.

Close the file: (close)

Write data (write)

Read data (read) (readlines) (readline)

Get the current read and write location (tell)

Navigate to a location (seek)

File system

Os, the standard library for file processing in Python, the following briefly describes the functions of the os library.

Os.getcwd () to view the current path

Os.mkdir () creates a subfolder that will report an error if it already exists

Os.rmdir () deletes a folder and will report an error if it does not exist.

Os.listdir () lists the names of all folders and files in the current directory or specified directory and returns a list

Os.rename () modifies the name of a file or folder

Os.remove () deletes a file

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

Servers

Wechat

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

12
Report