In real-world application development, the logic that we implement uses some data and those data may come from different sources like databases, files, or API. As a python developer, we should have good knowledge of handling these different sources. In this tutorial, we will see how we can handle files and perform some operations using python in-built functions.
Basically, a file is a location to store information in non-volatile memory (Hard Disk). And, it is characterized by its name and extension.
When we want to perform some operation(Read/Write/Update) in a file, we will open it first. Then we perform our task and finally, we will close it to save the changes and deallocate the resources tied with the file.
Similarly, in python, File operation takes place in the following order:
- Open a file
- Perform operation (Read/Write/Update)
- Close the file
- file_name_path → file name or path of the file to be opened.
- access_mode (Optional) → mode in which the file has to be opened. If not specified, default mode will be read.
- file_object → Object returned by open which is used for further file operations.
>>> f = open("Data.txt","r") # open file in current directory, read mode
>>> f = open("C:/Python/test.txt","w") # specifying full path, write mode
Access modes:- "r" → Read - Opens a file for reading (default), error if the file does not exist.
- "w" → Write - Opens a file for writing. Truncates the file if already exists or creates a new file if it doesn't exist.
- "a" → Append - Opens a file for appending at the end of the file without truncating. Creates a new file if it doesn't exist.
- "x" → Create - Creates the specified file, returns an error if the file exists.
- "+" → Opens a file for updating (reading and writing)
- "r+" → Read and write a file. Previous data will not be deleted.
- "w+" → Write and read a file. Override the existing data.
- "a+" → Append and read a file. Previous data will not be deleted.
- "t" → Text - Opens in text mode (default)
- "b" → Binary - Opens in binary mode.
>>> f = open("Data.txt", 'r') #Opens the file
# perform file operations
>>> f.close() #Closes the file
Even though this method closes the file. Using the close method straight away is not recommended, In case an exception occurs in the file operation (read or write) then the code will exit without closing the file.try:
f = open("Data.txt", 'r') #Opens the file
# perform file operations
finally:
f.close() #Closes the file
We can also use "with" statement to close a file Since this ensures that the file is closed when the block inside the "with" statement is exited. No need to call the close() method explicitly.with open("Data.txt", 'r') as f:
# perform file operations
Reading Files:f = open("Data.txt","r")
line1 = f.readline()
print(line1, end="")
line2 = f.readline()
print(line2, end="")
line3 = f.readline()
print(line3, end="")
f.close()
#result
First line: Hello World
Second line: Data file
Third line: Reading Operation
Reading a file contents into a list - readlines():f = open("Data.txt","r")
contents = f.readlines()
for line in contents:
print(line,end="")
f.close()
#result
First line: Hello World
Second line: Data file
Third line: Reading Operation
Reading contents of a file into a string - read(size):f = open("Data.txt","r")
data = f.read(10)
print(data)
f.close()
#result
First line
Writing to Files in Python:- To write a file in python, we can use the write() method. And also, we must need to specify the access mode "w"/"a" to the open() function.
- "w" mode overwrites the existing data if the file is already present or creates if the file doesn't exist. And, it will also return the number of characters written into a file.
f = open("Data.txt","r")
data = f.read()
print("Before Writing")
print(data)
f.close()
f = open("Data.txt","w")
num1 = f.write("Write operations performed\n")
num2 = f.write("lines written using write method\n")
print("Total number of characters written:",num1+num2)
f.close()
f = open("Data.txt","r")
data = f.read()
print("After Writing")
print(data)
f.close()
#result
Before Writing
First line: Hello World
Second line: Data file
Third line: Reading Operation
Total number of characters written: 60
After Writing
Write operations performed
lines written using write method
- Here, I have used the existing file with write mode, which results in overwriting the existing data, and the write method also returned the number of characters added to the file.
- And, We must include the newline characters explicitly to distinguish the lines.
- tell() → get the current position which is pointed by the file object within the file.
- seek() → Navigate the file object pointer to the required position specified.
No comments:
Post a Comment