Files in Python
File is an object used by Python to interact with the external files stored on your computer. These file objects can be any type of file you have on your computer, whether it be an audio file, a text file, emails, Excel documents, etc.
Let’s go step by step:
Opening a file
Let’s assume you have names.txt file on your computer in home path and it has following content:
james
steve
john
andrew
To open a file, we have to use open() method which returns file object and is most commonly used with two arguments: open(filename, mode)
.
>>> f =open('C:/users/ankit/Desktop/names.txt','a')
first argument is ‘path of a file’ and second argument is a ‘mode’.
- Different types of modes in which file can be opened:
- r
Default mode. Read only mode. Stream is positioned at the beginning of a file.
2. w
Write only mode. Creates a file if does not exist, overwrite a file if exists. Stream is positioned at the beginning of a file.
3. a
Open file for writing in append mode. Creates a file if does not exist. Stream is positioned at the end of a file.
4. x
Creates a new file if does not exist else fails.
5. r+
Open file for read and write mode, handles both actions when working with a file. Stream is positioned at the beginning of a file.
6. w+
Open file for read and write mode. Creates a file if does not exist, overwrite a file if exists. Stream is positioned at the beginning of a file.
7. a+
Open file for reading and writing in append mode. Creates a file if does not exist. Stream is positioned at the end of a file.
Reading a file
read() function which reads some quantity of data and returns it as a string.
readline() function will read a file line by line.
readlines() function will read whole file at a time.
>>>read_data = f.read() # reading a file
>>>print (read_data)
john
steve
james
andrew
>>> line = f.readline() # read first line
>>> print(line)
john
>>> line = f.readline() # read second line
steve
Now object f is pointing to the second line of a file(i.e. steve). To make it point to the starting of a file , use seek() method.
>>> f.seek(0) #file pointer is now pointing start of file
You can use loop to see the content of a file and is memory efficient.
>>> for line in open('names.txt'): #using loop to show file content
... print(line)
john
steve
james
andrew
Writing into a file
f.write(string)
writes the contents of string to the file.
Since, I have opened file in append mode, whatever I am writing into a file is getting appended to a file.
If you would have used write mode, then file would have been overwritten.
>>> f.write('\njohny')
>>> print(f.readlines())
john
steve
james
andrew
johny
Close a file
Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good practice to use the close() method to close a file and release a resource so that it will be used for another task.
To close a file:
>>> f.close()
Using with statement
Advantage of using this method is that any files opened will be closed automatically after you are done working with the file.
Open a file using with statement:
>>> with open('names.txt') as f:
... print(f.readlines())
...
['john\n','steve\n','james\n','andrew\n']
That’s it! Now you should be able to handle files.
Happy Coding!