Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator
os
directory

Useful functions of os module related to File/Directory in Python

Harsh Jain

In this shot, we will discuss some of the most useful functions in the Python os module to perform operations related to files and directories.

1. getcwd()

The ** getcwd() function** helps to find the current working directory – it returns a string with the complete path. Below is a code snippet to see how this works.

import os

print(os.getcwd())
getcwd() function in os module

Explanation:

  • In line 1, we import the required package.
  • In line 3, we use the getcwd() function and print our current working directory.

2. mkdir()

The mkdir() function is useful if you want to create a directory. This function accepts the path of your new directory that you wish to create. If you provide a path that already exists, the mkdir() function will throw a FileExistsError.

Take a look at the code snippet below to see how this works.

import os

current_dir = os.getcwd()
new_dir_path = current_dir + "/my_new_dir"

os.mkdir(path = new_dir_path)
print("New directory created successfully.")
mkdir() function in os module

Explanation:

  • In line 1, we import the package.
  • In line 3, we store the path of our current working directory in current_dir, as we are going to create my_new_dir directory in our current working directory.
  • In line 4, we create the path of our new directory.
  • In line 6, we pass the path to mkdir(), and our new directory is created.

If you replace path = new_dir_path to path = current_dir, you will see the FileExistsError being thrown.

Next, we will see how to change our working directory to the directory that we have just now created.

3. chdir()

The chdir() function changes your current directory to the path that you pass as an argument to this function. If you provide a path to a directory that does not exist, then you might get a FileNotFoundError. Similarly, if you are not authorized to access a directory, you will get a PermissionError. If you pass a path that is not a directory path, you may end up getting a NotADirectoryError.

Take a look at the code snippet below to see how this works.

import os

current_dir = os.getcwd()
new_dir_path = current_dir + "/my_new_dir"

os.mkdir(path = new_dir_path)

print("Working directory: ", os.getcwd())
os.chdir(new_dir_path)
print("New working directory: ", os.getcwd())
chdir() function in os module

Explanation:

  • The code above is almost the same as the previous code, the only difference is in line 9, where we used the chdir() function to change our working directory to the new directory that we just created.
  • We can verify that our working directory has been changed by printing it using getcwd(), as we did in lines 8 and 10.

4. listdir()

Now, let’s say that you have some files in a directory and you want to list all of them. In this case, the listdir() function comes in handy. This function will return all the files and directories that are present in the path that you can pass as an argument.

Take a look at the code snippet below to see how this works.

import os

current_dir = os.getcwd()
print(os.listdir(path = current_dir))
listdir() function in os module

Explanation:

  • In line 3, we set the path to our current working directory as we are going to list all the files that are present in our current working directory.
  • In line 4, we use the listdir() function to print all the files and directories present in the path that we passed as an argument to this function.

5. remove()

Now, let’s suppose that you want to remove a file from a directory – you could use the remove() function to perform this operation. To do so, you need to pass the complete path of the file that you want to delete.

This function will not delete a directory. If you pass a directory path to this function, an IsADirectoryError will be thrown. If the path of the file is not found, a FileNotFoundError is thrown.

Take a look at the code snippet below to see how this works.

import os

current_dir = os.getcwd()
file_name = "sample1.txt"
file_path = current_dir + "/" + file_name

print("Files before deletion: ", os.listdir(path = current_dir))
os.remove(path = file_path)
print("Files after deletion: ", os.listdir(path = current_dir))
remove() function in os module

Explanation:

  • From lines 3 to 5, we set the path of the sample1.txt file that we are going to delete.
  • In line 7, we print all the files and directories present in our working directory before deletion.
  • In line 8, we delete the sample1.txt file.
  • In line 9, we print all the files and directories present in our working directory after deletion. Here, we can see that the file is no longer present.

6. rmdir()

The remove() function only deletes a file, not a directory. If you wish to delete a directory, you can use the rmdir() function and pass the directory path to this function as an argument.

You can only delete the directory if it is an empty directory. If it’s not, first use remove() to delete all of the files, and then use rmdir() to delete the directory.

Take a look at the code snippet below to see how this works.

import os

current_dir = os.getcwd()
dir_name = "/my_new_dir"
dir_path = current_dir + dir_name

print("Files before deletion: ", os.listdir(path = current_dir))
os.rmdir(path = dir_path)
print("Files after deletion: ", os.listdir(path = current_dir))
rmdir() function in os module

Explanation:

  • The code is above almost the same as remove(). The only difference is that we pass the directory path to the rmdir() function.

These are the most used functions in os module that help to perform common file and directory relation operations using Python.

RELATED TAGS

python
communitycreator
os
directory
RELATED COURSES

View all Courses

Keep Exploring