While coding programs in Python, often it is required that the program save files in multiple directories or switch directories for file access during the execution of a code. For this purpose a lot of different directory-handling commands can be imported with the OS library in Python which is built-in.

There is an amplitude of directory manipulation and creation commands that do different tasks. Python is an implicitly defined language and does not require explicit declaration of commands and clauses, which makes working with files and directories much easier. However, due to this very reason, there should be a special emphasis on syntax and the use of different constructs that saves the code from un-needed exceptions and errors.

Making a New Directory

The OS library in python allows the user to utilize the built-in windows shell commands to manipulate files and directories such as mkdir, rmdir, etc.

import os

os.mkdir('dir1') #This will create a new directory/folder in your Python parent directory or wherever you are currently.

Changing Directories

Since it is learned that mkdir() will only create a directory in the current position of the system or the Python file, it is important to learn how it can be changed, it is quite a simple task which can be fulfilled by using the chdir() function.

import os

os.chdir('C://Users/home') #We can use both forward and back slash to separate the partitions or child-elements of the directories we want the system to point to.

Viewing Directories

Once you have learned how to create and change directory but you are not in the directory your pointer/system is currently located in, to solve this issue, you can simply use the getcwd() functions to display the current directory which is in use:

import os

os.getcwd() #This will retrieve the current directory's location in byte format and display it

print(os.getcwd()) # The print command will print the current directory's location and its address on the screen.

Deleting/Removing Directories

As it is already established that Python allows its user to use the same shell commands as Windows and Linux does for manipulating files and shell features, the Python OS library also includes the remove and rmdir() functions to delete or remove entire directories, the rmdir() command will only work on directories.

Assume that you have a folder named ‘test’ and it contains a file named ‘file1’, the below code will show how you can delete a single file and the entire directory. It is important that you have navigated to the partition or the location where that directory is located, if you want to delete a file you must navigate inside the directory using the chdir() function

import os

os.remove('file1.txt') #This will remove the file named 'file1.txt', mentioning the extension is important to allow the code to identify the correct file.

The following block of code will delete the entire directory, regardless of any data inside.

import os

os.rmdir('test') #This will delete the entire directory named 'test'

Joining and Splitting

You can join two paths together using Python to create a single working space for the Python code, this will allow you to avoid unnecessary path traversals through the code:

import os 

f1 = os.path.join('Users', 'Home') #This will join the separate path to users and home in a single variable.

In a similar fashion to Joining, Python also allows you to split the paths you have joined to work on them separately, here is an example:

import os

f1 = os.path.join("Users", "Home")

f2 = os.path.split(f1) # This will split the two paths, allowing for separate functions on each of them.

Listing Directory Content

There is also a command in Python which allows you to list all the content/data inside a directory:

import os

os.listdir() # This will list all files and content inside the directory in a linear fashion, this will only work on the current directory unless you define the directory path in the parenthesis.

os.walk() # This command will also walk through the directory and list all the content inside in a linear fashion. To access some other directory rather than the current one, it must be specified within the parenthesis.

Categorized in: