search
HomeBackend DevelopmentPython TutorialIn-depth understanding of python file reading and writing methods

1.open

After using open to open a file, you must remember to call the close() method of the fileobject. For example, you can use the try/finally statement to ensure that the file can be closed in the end.

file_object = open('thefile.txt')

try:

all_the_text = file_object.read( )

finally:

file_object.close( )

Note: You cannot put the open statement in the try block because it will appear when the file is opened. When an exception occurs, the file object file_object cannot execute the close() method.

2. Read file

Read text file

input = open('data', 'r')

#The second parameter defaults to r

input = open('data')

Read binary file

input = open('data', 'rb')

Read all content

file_object = open('thefile.txt')

try:

all_the_text = file_object.read( )

finally:

file_object.close( )

Read fixed bytes

file_object = open('abinfile', 'rb')

try:

while True:

chunk = file_object.read(100)

if not chunk:

                                                                                                                                                                                  ​

##Read each line

list

_of_all_the_lines = file_object.

readline

s( )

If the file is Text file, you can also directly traverse the file object to obtain each line:

for line in file_object: process line

3. Write files

Write text files

output = open('data', 'w')

Write binary files

output = open('data', 'wb')

Append writing file

output = open('data', 'w+')

Write data

file_object = open('thefile.txt', 'w')

file_object.write(all_the_text)

file_object .close( )

Write multiple lines

file_object.writelines(list_of_text_strings)

Note that calling writelines to write multiple lines will have a performance impact It is higher than using write to write once.

When processing log files, we often encounter such a situation: the log file is huge, and it is impossible to read the entire file into the memory for processing at one time. For example, it needs to be on a computer with a physical memory of 2GB. Processing a 2GB log file on the machine, we may want to process only 200MB of its content at a time.

In

Python

, the built-in File object directly provides a readlines(sizehint)

function

to accomplish such a thing. Take the following code as an example:

file = open('test.log', 'r')sizehint = 209715200 # 200M

position

= 0lines = file.readlines(sizehint)while not file.tell() - position Every time the readlines(sizehint) function is called, approximately 200MB of data will be returned, and all The returned data must be complete row data. In most cases, the number of bytes of the returned data will be slightly larger than the value specified by sizehint (except when the readlines(sizehint) function is called for the last time). Normally, Python will automatically adjust the user-specified sizehint value to an integer multiple of the internal cache size. file is a special type in python, which is used to operate external files in python programs. Everything in Python is an object, and file is no exception. File has file methods and attributes. Let's first look at how to create a file object: file(name[, mode[, buffering]])

The file() function is used to create a file object, which has an alias open() may be more vivid, they are built-in functions

. Let’s take a look at its parameters. Its parameters are all passed in the form of

string

. name is the name of the file.

mode is the open mode. The optional values ​​are r w a U, which represent read (default) and write. Add modes that support various line breaks. If you open a file in w or a mode, if the file does not exist, it will be created automatically. In addition, when using w mode to open an existing file, the content of the original file will be cleared, because the initial file operation mark is at the beginning of the file. If you perform a write operation at this time, the original content will undoubtedly be deleted. Erase it. Due to historical reasons, the newline character has different modes in different systems. For example, in Unix it is a \n, but in Windows it is '\r\n'. Opening a file in U mode supports all newline modes. In other words, '\r' '\n' '\r\n' can represent a newline, and there will be a tuple used to store the newline characters used in this file. However, although there are many modes for line breaks, when reading python, they are always replaced by \n. After the mode character, you can also add the two signs + b t, which respectively indicate that the file can be read and written at the same time and the file can be opened in binary mode or text mode (default).

buffering If it is 0, it means no buffering; if it is 1, it means "line buffering"; if it is a number greater than 1, it means the size of the buffer, which should be in bytes.

The file object has its own properties and methods. Let’s first look at the attributes of file.

closed #Mark whether the file has been closed, rewritten by close()

encoding #File encoding

mode #Open mode

name #File name

newlines #The newline mode used in the file is a tuple

softspace #boolean type, usually 0, which is said to be used for print

File reading and writing methods:

F.read([size]) #size is the length of the read, in bytes.

F.readline([size ])

#Read a line. If size is defined, it is possible to return only part of a line

F.readlines([size])

#Read each line of the file Acts as a member of a list and returns the list. In fact, it is implemented internally by calling readline() in a loop. If the size parameter is provided, size represents the total length of the read content, which means that only a part of the file may be read.

F.write(str)

#Write str to the file, write() will not add a newline character after str

F.writelines( seq)

#Write all the contents of seq to the file. This function also just writes faithfully, without adding anything after each line.

Other methods of file:

F.close()

#Close the file. Python will automatically close a file after it is no longer used. However, this function is not guaranteed. It is best to develop the habit of closing it yourself. If a file is operated on after it is closed, a ValueError will be generated

F.flush()

#Write the contents of the buffer to the hard disk

F.fileno()

#Returns a long integer "file label"

F.isatty()

#Whether the file is a terminal device file ( In unix systems)

F.tell()

#Returns the current position of the file operation mark, with the beginning of the file as the origin

F.next()

#Return to the next line and move the file operation flag to the next line. When a file is used in a statement such as for ... in file, the next() function is called to implement traversal.

F.seek(offset[,whence])

#Move the file operation mark to the offset position. This offset is generally calculated relative to the beginning of the file, and is generally a positive number. But this is not necessarily the case if the whence parameter is provided. whence can be 0 to start calculation from the beginning, and 1 to calculate from the current position as the origin. 2 means the calculation is performed with the end of the file as the origin. It should be noted that if the file is opened in a or a+ mode, the file operation mark will automatically return to the end of the file every time a write operation is performed.

F.truncate([size])

#Cut the file to the specified size. The default is to cut to the position of the current file operation mark. If size is larger than the file size, depending on the system, the file may not be changed, the file may be padded to the corresponding size with 0, or some random content may be added.


The above is the detailed content of In-depth understanding of python file reading and writing methods. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python vs. C  : Memory Management and ControlPython vs. C : Memory Management and ControlApr 19, 2025 am 12:17 AM

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python for Scientific Computing: A Detailed LookPython for Scientific Computing: A Detailed LookApr 19, 2025 am 12:15 AM

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Python and C  : Finding the Right ToolPython and C : Finding the Right ToolApr 19, 2025 am 12:04 AM

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python for Data Science and Machine LearningPython for Data Science and Machine LearningApr 19, 2025 am 12:02 AM

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Learning Python: Is 2 Hours of Daily Study Sufficient?Learning Python: Is 2 Hours of Daily Study Sufficient?Apr 18, 2025 am 12:22 AM

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Python for Web Development: Key ApplicationsPython for Web Development: Key ApplicationsApr 18, 2025 am 12:20 AM

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python vs. C  : Exploring Performance and EfficiencyPython vs. C : Exploring Performance and EfficiencyApr 18, 2025 am 12:20 AM

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)