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

How to use the open () function of Python

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use the open () function of Python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the open () function of Python.

1. File processing

The key function for using files in Python is the open () function.

There are four different ways to open a file (mode):

"r"-read-default value. Open the file for reading, and report an error if the file does not exist.

"a"-append-opens the file for append, and creates the file if it does not exist.

"w"-write-Open the file for writing, and create the file if it does not exist.

"x"-create-creates the specified file and returns an error if the file exists.

In addition, you can specify whether the file should be processed as binary or text mode.

"t"-text-default. Text mode.

"b"-binary-binary mode (for example, images).

The open () function takes two parameters: the file name and the pattern.

The syntax format of the open () function:

F=open ("a.txt", "r") # opens the a.txt file as read-only and creates a file object f.

Where f is equal to the a.txt file opened as read-only

You can enter the number of characters you want to read from the file in parentheses of the read () function. If there are no parameters in parentheses, all the contents of the file can be read by default.

Print (f.read ()) # read the whole contents of the file print (f.read (6)) # read the first 6 characters of the file

The readline () function reads the contents of an one-line file:

Print (f.readline ()) # read the first line # if you only want to read the first two lines, you can write: print (f.readline ()) print (f.readline ()) "reads one line in turn, and the content that has already been read will not be read repeatedly, so you can read two lines after writing twice."

The close () function closes the file:

Since open () opens the file, close () closes the file

Write the contents of the buffer to the file, close the file, and release the resources related to the file object.

After opening the file, close closes the file after editing and using the file

How to use the # close () function: f=open ("a.txt", "r") # Open the file print (f.read (5)) # read the first five characters print (f.readline ()) # and then read a line of characters f.close () # close the file 2.Python file writing after completing the use of the file

Write to an existing file:

To write to an existing file, you must add parameters to the open () function:

"a"-append-appends to the end of the file

"w"-write-will overwrite anything that already exists

Open the file "a.txt" and append the contents to the file

F=open ("a.txt", "a") # opening the file f.write ("Hello python!") # with "a" append writes appends the content to the end of the previous content when the file opened as "a" is written. F.close () # writes the contents of the buffer to the file and closes the file when it is finished. "" each run appends Hello python to the end of the file, and after three runs, there will be three sentences of Hello python ""

Open the file "a.txt" and overwrite the contents

F=open ("a.txt", "w") # opens the a.txt file f.write ("Hello python!") as "w" writes

For files opened in "w" mode, each write will overwrite the previous content. So no matter how many times you run it, there is only Hello python in the file!

F.close () writes the contents of the buffer to the file and closes the file. Releases resources related to the file object.

It is recommended that you close the file after each write, because the file is not necessarily saved into the file immediately, but there is a buffer.

When you close a file using the close () function, the contents of the buffer are written to the file.

Note: the "w" method overrides everything.

3. Create a new file

If you need to create a new file in python, use the open () method and the following method:

"x" create-A file will be created and an error will be returned if the file exists

"a" append-if the specified file does not exist, a file will be created

"w" write-if the specified file does not exist, a file will be created

Example:

Create a file named "b.txt":

F=open ("b.txt", "x") # create and open the b.txt file # if the file already exists, report an error to display the file to exist: FileExistsError: [Errno 17] File exists: 'Turtle programming test1.py' # if it does not exist, create a new file 4. Delete a file

If you need to delete a file, you must import the OS module and run its os.remove () function:

# Delete b.txt file import os # Import os module os.remove ("b.txt") # use the os.remove ("file name") function to delete the file.

Check to see if the file exists:

To avoid errors, you may need to check the existence of the file before attempting to delete it:

Example

Check that the file exists, and then delete it:

Import osif os.path.exists ("d.txt"): os.remove ("d.txt") else: print ("The file does not exist") delete folder

To delete the entire folder, use the os.rmdir () method:

Import os os rmdir ("b.txt") # Delete the entire folder

Note: only empty folders can be deleted

At this point, I believe you have a deeper understanding of "how to use the open () function of Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report