pwd
'C:\\Users\\bioinfo guru\\OneDrive - bioinfo.guru\\Documents\\python_book'
Saving and retrieving information is frequently required when working with computer programs. Python has methods to read and write files. In addition, there are some library that can facilitate working with specialized file types. For reading a file, a file handler need to be initated which is an object that can be use to work with file. A file handler incorporates information about the file along with its path and the mode in which the file is available for processing. The mode here refers to the manner in which the file would be opened i.e. for reading, writing, etc. The open
function takes name of the file as an argument along with mode and returns the corresponding file handler. This file handler object is iteratable and can be considered as a list having of all the lines in the file. Once done with reading the contents for the file, the file handler must be closed using the close
function.
pwd
'C:\\Users\\bioinfo guru\\OneDrive - bioinfo.guru\\Documents\\python_book'
# %load test_file.txt
is a test file for Python.
This file has .txt extension. This
# test_file.txt should be present in the current working directory.
= open('test_file.txt','r')
FH1 #print(FH1)
#print(f"The file {FH1.name} is open in {FH1.mode} mode.")
# print all the lines in the file.
for x in FH1:
print(x)
FH1.close()
This is a test file for Python.
This file has .txt extension.
Often it is useful to remove the newline character (\n
) at the end of the lines since the print fuction add as newline by default. This can be achieved using the rstrip()
function. This function without any argument remove the last character in the string and return a copy of the modified string.
For writing content to a file the open
function should be called with ‘w’ as the mode. When a file is opened in the write mode, a new file is created. In case there the file already exists then its contents are overwritten (without warning!). write
function is used to write content to a file. We can also use print
with file
attribute to write to file instead of printing on screen.
= open('temp_file.txt', 'w')
FH_out "This is the first sentence.\n")
FH_out.write(print("This is the second sentence.", file=FH_out)
FH_out.close()
# %load temp_file.txt
is the first sentence.This is the second sentence. This
= open('temp_file.txt','r')
FH2 for lines in FH2:
print(lines)
FH2.close()
This is the first sentence.
This is the second sentence.
To append contents to an existing file, it should be opened with ‘a’ option instead of ‘w’.
= open('temp_file.txt','a')
FH_out "This is the third sentence.\n")
FH_out.write("This is the \t fourth sentence.\n")
FH_out.write( FH_out.close()
= open('temp_file.txt','r')
FH3 for lines in FH3:
= lines.rstrip('\n')
lines print(lines)
FH3.close()
This is the first sentence.
This is the second sentence.
This is the third sentence.
This is the fourth sentence.
The readlines
function can be used to get a list having lines of the file as elements.
= open('temp_file.txt','r')
FH4 = FH4.readlines()
all_lines print(all_lines)
FH4.close()
['This is the first sentence.\n', 'This is the second sentence.\n', 'This is the third sentence.\n', 'This is the \t fourth sentence.\n']
with
keywordWe can also read and write file using the with
keyword. Here some action are performed on the file object within the with
block. This approach automatically closes the when the with
block is over.
with open("temp123.txt", "w") as FH_OUT:
print("Hello", file=FH_OUT, end=" ")
print("World!", file=FH_OUT)
print("Done")
Done
with open("temp123.txt", "a") as FH_OUT:
print("hi", file=FH_OUT)
with open("temp123.txt","r") as FH:
for line in FH.readlines():
print(line)
Hello World!
hi
Quiz: Write a program to print the third line of a text file.
#temp_file.txt should be there in the current directory
= open('temp_file.txt','r')
FH = FH.readlines()
all_lines print(all_lines[2])
This is the third sentence.