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 basic operating methods of Python files

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article Xiaobian for you to introduce in detail "what are the basic operations of Python files", the content is detailed, the steps are clear, the details are handled properly, I hope that this article "what are the basic operating methods of Python files" can help you solve your doubts, the following follows the editor's ideas slowly in depth, together to learn new knowledge bar.

How files are stored

In a computer, files are saved on disk in binary form

Text files and binary files

Text file

You can use text editing software to view

It's essentially a binary file.

Binary file

The saved content is not for people to read directly, but for other software to use

Binary files cannot be viewed using file editing software

Basic operation of file

Routines for manipulating files

There are three steps to manipulate files on your computer:

1. Open a file

two。 Read and write documents

To read the contents of a file into its contents.

Write writes the contents of memory to a file

3. Close the file

Functions / methods for manipulating files

The ordinal function / method indicates that 1open opens the file and returns the file Operand 2read to read the contents of the file into memory 3write writes the specified contents to the file 4close closes the file

The open function is responsible for opening the file and returning the file object

All three methods of read / write / close need to be called through the file object.

Read method-- read the file

The first argument to the open function is the name of the file to open (the file name is case sensitive)

If the file exists, return the file Operand

If the file does not exist, an exception is thrown

The read method can read in and return all the contents of the file at once

The close method is responsible for closing the file

If you forget to close the file, it will consume the system resources and affect the subsequent access to the file.

Note: after the method is executed, the file pointer is moved to the end of the file

Example:

# 1. Open the file file = open ("read.txt") # 2. Read the file contents text = file.read () print (text) # 3. Close the file file.close ()

Note:

In development, code for opening and closing is usually written first, and then read / write operations for files are written in the middle.

File pointer

Where does the file pointer mark start reading data?

When you first open a file, the file pointer usually points to the beginning of the file

When the read method is executed, the file pointer moves to the end of the read

Moves to the end of the file by default

Note: if you execute the read method once and read everything, calling the read method again will not read anything, because after the first read, the file pointer moves to the end of the file, and the call again will not read anything.

How to open a file

The open function opens the file read-only by default and returns the file object.

The syntax is as follows:

F = open ("file name", "access method") access method indicates that r opens the file read-only. The pointer to the file will be placed at the beginning of the file, which is the default mode. If the file does not exist, an exception w is thrown to open the file in write-only mode. If the file exists, it will be overwritten. If the file does not exist, create a new file a to open the file 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 new file to write to r + open the file in read-write mode. The pointer to the file will be placed at the beginning of the file. If the file does not exist, an exception w + is thrown to open the file in read-write mode. If the file exists, it will be overwritten. If the file does not exist, create a new file a+ to open the file in read-write mode. 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 new file to write to

Example:

Open the file in write-only mode:

# 1. Open the file file = open ("read.txt", "w") # and open the file as write-only. If the file exists, it will be overwritten. If the file does not exist, create a new file # 2. Write the file file.write ("vvcat") # 3. Close the file file.close ()

Open the file as an append:

# 1. Open the file file = open ("read.txt", "a") # and open the file as an append 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 new file to write to # 2. Write the file file.write ("vvcat") # 3. Close the file file.close ()

Note:

Frequently moving the file pointer will affect the reading and writing efficiency of the file.

Read the contents of the file by line

By default, the read method reads all the contents of the file into memory at once.

If the file is too large, the use of memory will be very serious.

Readline method

The readline method can read one line at a time

Method is executed, the file pointer is moved to the next line, ready to read again

Example:

# Open the file file = open ("read.txt") while True: # read a line text = file.readline () # determine whether you read the content if not text: break # there is already a''print (text, end= ") at the end of each line read # close the file file.close ()

Copy a file

Small file replication

Open an existing file, read the full contents, and write to another file

# 1. Open the file file_read = open ("read.txt") file_write = open ("read [copy] .txt", "w") # 2. Read and write text = file_read.read () # read the contents of the file file_write.write (text) # write the read to the new file # 3. Close the file file_read.close () file_write.close ()

Large file copy

Open an existing file, read the contents line by line, and write to another file sequentially

# 1. Open the file file_read = open ("read.txt") file_write = open ("read [copy] .txt", "w") # 2. Read and write while True: # read a line of content text = file_read.readline () # to determine whether to read the content if not text: break file_write.write (text) # 3. Close the file file_read.close () file_write.close ()

Common management operations of files / directories

In Python, you need to perform regular file / directory management operations: create, rename, delete, change path, view directory contents. Need to import os module

File operation

Serial number method name description sample 1rename rename file os.rename (source file name, target file name) 2remove delete file os.remove (file name)

Directory operation

Serial number method name description sample 1listdir directory list os.listdir (directory name) 2mkdir create directory os.mkdir (directory name) 3rmdir delete directory os.rmdir (directory name) 4getcwd get current directory os.getcwd () 5chdir modify working directory os.chdir (target directory) 6path.isdir determine whether it is a directory os.path.isdir (file path)

Note: both relative and absolute paths are supported for file or directory operations.

After reading this, the article "what are the basic operating methods of Python documents" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.

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