Like other languages, python also supports file handling. Using open() function in python we can open files in read or write mode. open() function accepts two arguments 1. filename (File name is nothing but which file to be read or write) 2. mode (Mode is nothing but in which mode file to be opened). We have different modes in python, popular modes are listed below. open() function will return an object. One thing we must keep in mind that "Mode" argument is not mandatory for open() function, "r" read mode is the default argument
"r" - Read
"w" - Write
"a" - Append
"w+" - Both Read and Write
Reading a File: To read a file, we need to open a file in "r" or "r+" modes.
obj = open("sample.txt", "r")
for line in obj:
print(line)
Output: In sample.txt we have two lines, so those two lines are printed
This is a sample file
In this file we dont discuss anything
We have other ways to read to a file as well, read() and readlines() methods
read(): This method gives output in a string format (Entire file as a string and also we can also specify the number of characters to be read.
Program: 1
obj = open("sample.txt", "r")
output = obj.read()
print(type(output))
print(output)
Output:
<class 'str'>
This is a sample file
In this file we dont discuss anything
Program: 2
obj = open("sample.txt", "r")
output = obj.read(6)
print(output)
Output:
This i
readlines(): This method gives output in a list format, like wise in read() method here also we can specify number of lines to be read.
Program: 1
obj = open("sample.txt", "r")
output = obj.readlines()
print(type(output))
print(output)
Output:
<class 'list'>
['This is a sample file\n', 'In this file we dont discuss anything\n']
Program: 2
obj = open("sample.txt", "r")
output = obj.readlines(1)
print(output)
output:
['This is a sample file\n']
Writing a file: To write to a file, we need to open a file in "w" / "a" / "r+" modes.
obj = open("sample.txt", "w")
obj.write("This is a new line")
obj.close()
close() method is used to clear the resources to that program
Notes:
- In "w" mode, file will be overridden, That means old data won't be there and also if file not exists it will create a new file.
- In "a" mode, file will be appended with new data and also if file not exists it will create a new file.
- In "w+" mode, file will be overridden, That means old data won't be there and also if file not exists it will create a new file.
With Statement in Python: With statement ensures the proper closing of the file, when we do file writing and also code looks compact and more readable. If file not closed properly, we will may face many issues or incorrect behaviour. When we use with statement in file handling, no need write close() method, As mentioned above with statement ensures closing the file. Also with statement used in other areas like socket and subprocess programming.
with open("sample.txt", "w") as obj:
obj.write("Adding data without close method")
The above code is equivalent to the below code.
obj = open("sample.txt", "w")
obj.write("Adding data without close method")
obj.close()