Learning Python Part-21: Python Files and Directories

Python Files:

File is a named location on disk to store related information. It is used to store data in a non-volatile memory (e.g. hard disk) persistently. Since, random access memory (RAM) is volatile memory, it loses data whenever computer is turned off or rebooted, so we use files for storing the data.

When we want to read from or write to a file we need to open it first. When we are done reading or writing a file, it needs to be closed, so that resources that are tied with the file are released.
Hence, in Python, a file operation takes place in the following order.

  • Open a file
  • Read or write (perform operation)
  • Close the file

How to open a file?:

Python has a built-in function called as open() to open a file. This function returns a file object, known as handle, as it is used to read or modify the file accordingly.

f = open(“test.txt”)               # open file in current directory
f = open(“C:/test.txt”)          # specifying full file path

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 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, binary mode returns bytes and this is the mode to be used when dealing with non-text files like image or exe files.

Python File Modes:

Unlike other languages, the character ‘a’ does not imply the number 97 until it is encoded using ASCII (or other equivalent encodings). Moreover, the default encoding is platform dependent.  In Windows OS, it is CP1252 but UTF-8 in Linux OS. So, we must not rely on the default encoding or else code will behave differently in different platforms. Hence, when working with files in text mode, it is highly recommended to specify the encoding type.

f = open(“test.txt”, mode = ‘r’, encoding = ‘utf-8’)

How to close a file Using Python?:

  • Once we are done with operations to the file, we need to properly close the file.
  • Closing a file is done using Python close() method.
  • Python has a garbage collector to clean up unreferenced objects but, we must not rely on it to close the file.

f = open(“test.txt”, mode = ‘w’, encoding = ‘utf-8’) 

# perform file operations 

  • This method is not entirely safe. If an exception occurs when we are performing some operation with the file, the code exits without closing the file.
  • A safer way is to use a try…finally block.


     f = open(“test.txt”,encoding = ‘utf-8’) 
  • This guarantees that the file is properly closed even if an exception is raised, causing program flow to stop.
  • Another way to do this is using the with statement. This ensures that the file is closed when the block inside with is exited.
  • We don’t need to explicitly call the close() method. It is done internally.

with open(“test.txt”, mode = ‘w’, encoding = ‘utf-8’) as file:    

How to read files in Python?

  • To read a file in Python, we must open the file in reading mode which is a default mode if not defined while opening a file.
  • There are various methods available for this purpose. 
  • We can use the read(size) method to read in size number of data. 
  • If size parameter is not specified, it reads and returns up to the end of the file.

 file = open(“test.txt”,’r’,encoding = ‘utf-8’)
 file.read(4)             # read the first 4 data Output:  ‘first’ 

 file.read(4)             # read the next 4 data Output: ‘line
 file.read()               # read rest till end of fileOutput:  ‘first line\nsecond line\nthird lines\n’
 file.read()               # further reading returns empty string ‘’
  • We can change file cursor position using the seek() method. 
  • Similarly, the tell() method returns our current position (in number of bytes).

file.tell()                    # get the current file position 32
file.seek(0)                # bring file cursor to initial position 0
print(file.read())        # read the entire file

# Output

first line
second line
third lines

  • We can read a file line-by-line using a for loop. This is both efficient and fast.

for line in file:
      print(line, end = ’’)

# Output

first line
second line
third lines
  • Alternately, we can also use readline() method to read individual lines of a file. 
  • This method reads a file till the newline, including the newline character.
>>> file.readline()
‘first line\n’

>>> f.readline()
‘second line\n’

>>> f.readline()
‘third lines\n’

>>> f.readline()

How to write to File Using Python?

  • In order to write into a file in Python, we need to open it in ‘w’, ‘a’ or  ‘x’ mode.
    • w – Write mode
    • a – Append mode
    • x – Exclusive creation mode
  • We need to be careful with the ‘w’ mode as it will overwrite into the file if it already exists and all of previously written data is erased.
  • Writing a string or sequence of bytes (for binary files) is done using write() method. 
  • This method returns the number of characters written to the file.

with open(“test.txt”, mode = ‘w’, encoding = ‘utf-8’) as file:

file.write(“First line\n”)
file.write(“second line\n\”)
file.write(“third line\n”)

  • This program will create a new file named ‘test.txt’ if it does not exist. If it does exist, it is overwritten.
  • We must include the newline characters ourselves to distinguish different lines.

Python File Methods:

Python Directory and Files Management:

  • If there are large number of files to handle in your Python program, you can arrange your code within different directories for better manageability.
  • A directory or folder is a collection of files and sub directories. 
  • Python has the os module, which provides us with many useful methods to work with directories and files.

Get Current Directory:

  • We can get the present working directory using the getcwd() method.
  • This method returns the current working directory in the form of a string. 
  • We can also use the getcwdb() method to get it as bytes object.
  •  Output

Changing Directory:

  • We can change the current working directory using the chdir() method.
  • The new path that we want to change to must be supplied as a string to this method. 
  • We can use both forward slash (/) or the backward slash (\) to separate path elements.
  • It is safer to use escape sequence when using the backward slash.

List Directories and Files:

  • All files and sub directories inside a directory can be listed using the listdir() method.
  • This method takes in a path and returns a list of sub directories and files in that path. 
  • If no path is specified, it returns from the current working directory.

Making a New Directory:

  • We can create a new directory using the mkdir() method.
  • This method takes in the path of the new directory. 
  • If the path is not specified, the new directory is created in the current working directory.



Renaming a Directory or a File:

  • The rename() method is used to rename a directory or file.
  • The first argument is the old name and the new name must be supplies as the second argument.



Removing Directory or File:

  • A file can be removed  using the remove() method.
  • Similarly, the rmdir() method removes an empty directory.

  • However, note that rmdir() method can only remove empty directories.
  • In order to remove a non-empty directory we can use the rmtree() method inside the shutil module. 

import shutil

Leave a Reply