File Handling

Navya Gunda
4 min readJun 19, 2021

This blog is about using and working with python files

Files :

A file is a computer resource that can be used to store data in a device, there are several types of files which are used for different purposes. A file may be designed to store a picture, a written message, a video, a computer program, or a wide variety of other kinds of data. You can tell what type of data is being stored in the file by its extension, .txt means that it is a text file, .py shows that it is a python file, .jpg means that it is an image, etc. By using computer programs, we can open, read, change, save, and close a computer file. Files are useful because they will store data permanently unlike in RAM.

A file operation takes place in the following order :

  1. Open the file
  2. Read or Write in the file
  3. Close the file

Opening a file :

Python has a built-in open() function to open a file

This function returns a file object so that it can be used to read or modify the file accordingly.

Syntax for opening a file :

variable_name=open("filename","mode")

File paths :

When you pass a simple filename like pi_digits.txt to the open() function, Python looks in the directory where the file that’s currently being executed is stored.

Sometimes, depending on how you organize your work, the file you want to open won’t be in the same directory as your program file.

For example, you might store your python program in a folder called python_work; inside python_work, you might have another folder called text_files to distinguish your program files from the text files they’re manipulating. Even though text_files is in python_work, just passing open() the name of a file in text_files won’t work, because Python will only look in python_work and stop there.

To get Python to open files from a directory other than the one where your program file is stored, you need to provide a file path, which tells Python to look in a specific location on your system. Because text_files is inside python_work.

An absolute or full path points to the same location in a file system, regardless of the current working directory.

Modes :

We can specify the mode while opening a file. In mode, we specify whether we want to read r, write w or append a to the file. We can also specify if we want to open the file in text mode or binary mode.

The default is reading in text mode. In this mode, we get strings when reading from the file

On the other hand, write mode writes a string in your file.

Below are the modes that you can open your file in :

Closing a file :

Once you are done using the python file you have to close it to make sure all of the data gets saved.

When using a python file several resources get attached to it, you have to release those resources by using the close() method.

a=open("Names.txt")
a.close()

Reading a file :

To read a python file we have to open it in read mode which is symbolized as an r.

To read the size number of the data in a file we can use the read(size) method, if a parameter is not specified it will read to the end of the file.

b=open("Names.txt",'r')
b.read(4) #reads the first 4 data
b.read() #read in the rest till end of file

The read() method returns a newline as \n. Once the end of the file is reached, we get an empty string on further reading

You can change our current file cursor (position) using the seek() method.Similarly, the tell() method returns our current position

b.seek(0)
b.tell()

We can also use the readline() method to read individual lines in a file. This function reads a file until it finds a \n which identifies a new line.

b.readline()

There is also another method called readlines(), it returns a list of remaining lines of the entire file.

b.readlines()

Writing in a file :

To write in a file using python we have to open the file in write mode which is symbolized as a w.

We have to carefully use write mode, because it will overwrite into a file if it already exists. Due to this, all the previous data stored in the file gets erased.

Writing strings or bytes is done using the write() method. This method returns the number of characters written into the file.

c=open("test.txt",'w')
c.write("My first file\n")
c.close()

This program will create a new file named test.txt in the current directory if it does not exist. If it does exist, the write method will overwrite it.

Appending Files :

To not overwrite data in an existing file you can open a file in append mode. When opening a file in append mode the file cursor will be at the end of the file and any string that to write to a file gets added at the end.

d=open("test.txt",'a')
d.write("This is an appended text\n")
d.close()

--

--