Python - Opening/Close Files -- How to read a file in Python -- Filereader/scanner(file) equvivalent - readlines()


Opening Files



You can use Python to read and write the contents of files.
Text files are the easiest to manipulate. Before a file can be edited, it must be opened, using the open function.myfile = open("filename.txt")
The argument of the open function is the path to the file. If the file is in the current working directory of the program, you can specify only its name.

You can specify the mode used to open a file by applying a second argument to the open function.
Sending "r" means open in read mode, which is the default. 
Sending "w" means write mode, for rewriting the contents of a file.
Sending "a" means append mode, for adding new content to the end of the file.

Adding "b" to a mode opens it in binary mode, which is used for non-text files (such as image and sound files).
For example:# write mode
open("filename.txt", "w")

# read mode
open("filename.txt", "r")
open("filename.txt")

# binary write mode
open("filename.txt", "wb")

You can use the + sign with each of the modes above to give them extra access to files. For example, r+ opens the file for both reading and writing.

Once a file has been opened and used, you should close it.
This is done with the close method of the file object.file = open("filename.txt", "w")
# do stuff to the file
file.close()


The contents of a file that has been opened in text mode can be read using the read method.file = open("filename.txt", "r")
cont = file.read()
print(cont)
file.close()

This will print all of the contents of the file "filename.txt".

To read only a certain amount of a file, you can provide a number as an argument to the read function. This determines the number of bytes that should be read. 
You can make more calls to read on the same file object to read more of the file byte by byte. With no argumentread returns the rest of the file.file = open("filename.txt", "r")
print(file.read(16))
print(file.read(4))
print(file.read(4))
print(file.read())
file.close()

Just like passing no arguments, negative values will return the entire contents.

After all contents in a file have been read, any attempts to read further from that file will return an empty string, because you are trying to read from the end of the file.file = open("filename.txt", "r")
file.read()
print("Re-reading")
print(file.read())
print("Finished")
file.close()

Result:>>>
Re-reading

Finished
>>>



To retrieve each line in a file, you can use the readlines method to return a list in which each element is a line in the file.
For example:file = open("filename.txt", "r")
print(file.readlines())
file.close()

Result:>>>
['Line 1 text \n', 'Line 2 text \n', 'Line 3 text']
>>>

You can also use a for loop to iterate through the lines in the file:file = open("filename.txt", "r")

for line in file:
print(line)

file.close()

Result:>>>
Line 1 text

Line 2 text

Line 3 text
>>>

In the output, the lines are separated by blank lines, as the print function automatically adds a new line at the end of its output.









Comments

Popular posts from this blog

Python - Argument Passing

Python - How to Escape special characters in String