2.1 KiB
Python File Handling Operations
This document explains common operations performed on files in Python.
Opening a File
The open() function is used to open a file. It takes two main arguments: the file name and the mode.
f = open("file.txt")
Reading File Content
There are several ways to read the content of a file:
f.read(): Reads the entire content of the file as a single string.f.readlines(): Reads all lines of the file and returns them as a list of strings.f.read()followed byf.read(): This sequence reads the entire file content twice, the second time returning an empty string.with open(...) as f:: This context manager automatically closes the file after the block of code is executed, even if errors occur. This is the recommended way to work with files.
with open("file.txt") as f:
lines = f.readlines()
print(lines[-1])
This code opens the file "file.txt", reads all lines into a list called lines, and then prints the last line of the file.
Closing a File
It's important to close the file after you are done with it to release system resources.
file = open("file.txt" , 'r') # read
file.read()
file.close()
Writing to a File
You can write to a file using the open() function with the 'w' (write) or 'a' (append) modes.
'w'(write): Opens the file for writing. If the file exists, its content is overwritten. If the file does not exist, it creates a new file.'a'(append): Opens the file for appending. New data is added to the end of the file. If the file does not exist, it creates a new file.
file = open("file.txt" , 'r') # read
file.read()
file.close()
file = open("file.txt" , 'w') # write
file.write("This Is Write Message")
file.close()
file = open("file.txt" , 'a') # append
file.write("This Is Append Message")
file.close()
This code first reads the file, then opens it in write mode and writes the string "This Is Write Message", overwriting any existing content. Finally, it opens the file in append mode and adds the string "This Is Append Message" to the end of the file.