Home >Backend Development >Python Tutorial >How to use the write() function to write content to a file in Python 2.x
How to use the write() function to write content to a file in Python 2.x
In Python 2.x, we can use the write() function to write content to a file. The write() function is one of the methods of the file object and can be used to write string or binary data to the file. In this article, I will detail how to use the write() function and some common use cases.
Before writing to a file using the write() function, we need to open a file first. You can use the open() function to open a file and operate by specifying the file name and opening mode. The filename can be an absolute or relative path, and the opening mode can be "r" (read-only mode) or "w" (write mode).
# 打开文件 file = open("example.txt", "w")
In the above code, we use the open() function to open a file named "example.txt" and specify the opening mode as "w", which is the write mode. If the file does not exist, Python will automatically create a new file. If the file already exists, the original content will be overwritten.
Once the file is open, we can use the write() function to write the content. The write() function accepts a string as a parameter and writes it to the file. The write() function can be called multiple times in succession to write content to different locations in the file.
# 写入内容 file.write("Hello, World! ") file.write("This is an example.")
In the above code, we called the write() function twice in succession to write the strings "Hello, World!
" and "This is an example." into the file. It should be noted that the write() function does not automatically add a newline character at the end of each line, it needs to be added manually.
After completing the file writing operation, we should close the file in time to release resources and ensure data preservation. Files can be closed using the close() function.
# 关闭文件 file.close()
After closing the file, trying to write to the file again will cause an exception. Therefore, it is important to ensure that the file is closed after the write operation is complete.
The following is a complete example that demonstrates how to use the write() function to write content to a file.
# 打开文件 file = open("example.txt", "w") # 写入内容 file.write("Hello, World! ") file.write("This is an example.") # 关闭文件 file.close()
In the above example, we opened a file named "example.txt" and wrote two lines to it. After completing the write operation, the file is closed.
Summary
Use the write() function to easily write content to a file. We just need to open the file, use the write() function to write the required content, and finally close the file. When using the write() function, you need to pay attention to adding newlines and closing the file in time to ensure that the written content is saved correctly. By mastering the use of the write() function, we can easily perform file writing operations.
The above is the detailed content of How to use the write() function to write content to a file in Python 2.x. For more information, please follow other related articles on the PHP Chinese website!