Python Directory and Files Management#

Learn about file and directory management in Python, i.e. creating a directory, renaming it, listing all directories, and working with them.

Python Directory#

If there are a large number of files to handle in our Python program, we can arrange our code within different directories to make things more manageable.

A directory or folder is a collection of files and subdirectories. Python has the os module that provides us with many useful methods to work with directories (and files as well).

Get Current Directory getcwd() -#

We can get the present working directory using the getcwd() method of the os module.

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

import os
print(os.getcwd())
import os
os.getcwdb()

The extra backslash implies an escape sequence. The print() function will render this properly.

Changing Directory chdir() -#

We can change the current working directory by using the chdir() method.

The new path that we want to change into must be supplied as a string to this method. We can use both the forward-slash / or the backward-slash \ to separate the path elements.

It is safer to use an escape sequence when using the backward slash.

Syntax:

os.chdir("newdir")

Remember to:

  1. First copy the path for current working directory

  2. Windows user and MacOS users, first create an empty folder with name β€œxyz” on your desktop.

import os

# Changing a directory to "C:\Users\Anukool\OneDrive\Desktop\xyz"
os.chdir(r"C:\Users\Anukool\OneDrive\Desktop\xyz") 
  
print("Directory changed") 

print(os.getcwd())
import os
print(os.getcwd())
import os

# Changing a directory back to original directory "C:\Users\Anukool\01_Learn_Python4Data\05_Python_Files"
os.chdir(r"C:\Users\Anukool\01_Learn_Python4Data\05_Python_Files") 
  
print("Directory changed") 
Directory changed
import os
print(os.getcwd())

List Directories and Files listdir() -#

The listdir() method displays all files and sub-directories inside a directory.

This method takes in a path and returns a list of subdirectories and files in that path. If no path is specified, it returns the list of subdirectories and files from the current working directory.

print(os.getcwd())

os.listdir()
os.listdir('C:\\')

Making a New Directory mkdir() -#

You can use the mkdir() method of the os module to create directories in the current directory. You need to supply an argument to this method, which contains the name of the directory to be created.

This method takes in the path of the new directory. If the full path is not specified, the new directory is created in the current working directory.

Syntax:

os.mkdir("dir_name")

import os
os.mkdir('python_study')
print("Directory created") 

os.listdir()
Directory created
['.ipynb_checkpoints',
 '001_Python_File_Input_Output.ipynb',
 '002_Python_File_Directory.ipynb',
 '003_Python_File_Exception.ipynb',
 '004_Python_Exceptions_Handling.ipynb',
 '005_Python_User_defined_Exceptions.ipynb',
 'data.txt',
 'data_1.txt',
 'img',
 'logo.png',
 'logo1.png',
 'python_study',
 'test.txt',
 'testfile',
 'test_1.txt',
 'test_2.txt']

Renaming a Directory or a File rename() -#

The rename() method can rename a directory or a file.

Syntax:

os.rename(current_file_name, new_file_name)

os.listdir()

os.rename('python_study','python_learning')
print("Directory renamed") 

os.listdir()
Directory renamed
['.ipynb_checkpoints',
 '001_Python_File_Input_Output.ipynb',
 '002_Python_File_Directory.ipynb',
 '003_Python_File_Exception.ipynb',
 '004_Python_Exceptions_Handling.ipynb',
 '005_Python_User_defined_Exceptions.ipynb',
 'data.txt',
 'data_1.txt',
 'img',
 'logo.png',
 'logo1.png',
 'python_learning',
 'test.txt',
 'testfile',
 'test_1.txt',
 'test_2.txt']
import os
os.rename('data_1.txt','my_data.txt')
print("file renamed") 
file renamed

Removing a Directory or a File remove() and rmdir() -#

A file can be removed (deleted) using the remove() method. Similarly, the rmdir() method removes an empty directory. Before removing a directory, all the contents in it should be removed.

import os

# This would remove "C:\Users\Anukool\OneDrive\Desktop\xyz" directory.
os.rmdir(r"C:\Users\Anukool\OneDrive\Desktop\xyz")
print("Directory deleted") 

os.listdir()
Directory deleted
['.ipynb_checkpoints',
 '001_Python_File_Input_Output.ipynb',
 '002_Python_File_Directory.ipynb',
 '003_Python_File_Exception.ipynb',
 '004_Python_Exceptions_Handling.ipynb',
 '005_Python_User_defined_Exceptions.ipynb',
 'data.txt',
 'img',
 'logo.png',
 'logo1.png',
 'my_data.txt',
 'python_learning',
 'test.txt',
 'testfile',
 'test_1.txt',
 'test_2.txt']
import os
os.remove('my_data.txt')
print("file deleted") 
file deleted

Note: The 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

shutil.rmtree('python_learning')
os.listdir()
['.ipynb_checkpoints',
 '001_Python_File_Input_Output.ipynb',
 '002_Python_File_Directory.ipynb',
 '003_Python_File_Exception.ipynb',
 '004_Python_Exceptions_Handling.ipynb',
 '005_Python_User_defined_Exceptions.ipynb',
 'data.txt',
 'img',
 'logo.png',
 'logo1.png',
 'test.txt',
 'testfile',
 'test_1.txt',
 'test_2.txt']