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.
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 osprint(os.getcwd())
Explanation:
getcwd()
function and print our current working directory.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 oscurrent_dir = os.getcwd()new_dir_path = current_dir + "/my_new_dir"os.mkdir(path = new_dir_path)print("New directory created successfully.")
Explanation:
current_dir
, as we are going to create my_new_dir
directory in our current working directory.path
to mkdir()
, and our new directory is created.If you replace
path = new_dir_path
topath = 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.
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 oscurrent_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())
Explanation:
chdir()
function to change our working directory to the new directory that we just created.getcwd()
, as we did in lines 8 and 10.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 oscurrent_dir = os.getcwd()print(os.listdir(path = current_dir))
Explanation:
listdir()
function to print all the files and directories present in the path
that we passed as an argument to this function.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, aFileNotFoundError
is thrown.
Take a look at the code snippet below to see how this works.
import oscurrent_dir = os.getcwd()file_name = "sample1.txt"file_path = current_dir + "/" + file_nameprint("Files before deletion: ", os.listdir(path = current_dir))os.remove(path = file_path)print("Files after deletion: ", os.listdir(path = current_dir))
Explanation:
sample1.txt
file that we are going to delete.sample1.txt
file.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 usermdir()
to delete the directory.
Take a look at the code snippet below to see how this works.
import oscurrent_dir = os.getcwd()dir_name = "/my_new_dir"dir_path = current_dir + dir_nameprint("Files before deletion: ", os.listdir(path = current_dir))os.rmdir(path = dir_path)print("Files after deletion: ", os.listdir(path = current_dir))
Explanation:
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.