Python Directory - Create, Rename, Delete, Listing, Change With Examples

Good day, learners! In this tutorial we are going to learn about Python Directory. In our previous tutorial, we learned about Python File.

Python Directory

Python Directory is basically working with operating system directories. To work with Python Directories, we need to import os module. This os modules contains functions to create, view, delete, rename directories.

Basically, there are some common functions to access Python Directories. The functions are given below

  1. getcwd(): This function returns a string containing current directory from where to code is being executed.
  2. listdir(location): This function returns a list string containing current the names of current directories.
  3. chdir(location): This function changes the current directory to the given location
  4. mkdir(name): This function creates a new directory labeling with the given name.
  5. rename(old_name,new_name): This function renames the old_name directory to new_name directory

Using these functions, you can do basic operation with python directories. But, to show you example, some sample code will be given to help you understand about the implementation.

Getting List of Directories

You can get the list of directories on a specific location. To do so, you have to to use listdir(location) function. If you put the location, the function will return a list of string containing the names of the directories of the given location. The following code will help you understand the thing

The output of the following code will be:

Which is same as the actual picture.

Python Directory

Getting The Location of Current Directory

As we said earlier, you can get the location of the current directory you’re in by using getcwd() function. The following code will illustrate you the idea

The output of the following code will be

Similarly, you can implement all those functions that are mentioned above. Try those, give yourself a challenge!

Why Do We Need Python Directories

Reading this tutorial, it may come to your mind that why we need Python Directories. In this section we will going to discuss this thing.

Suppose, you are making some a software using Python where you need to read/write files from different directories. The directories can be dynamic so that you cannot fix the directory from your code, rather you need to choose the directory dynamically. After choosing the directory, you may have to create a new directory or write a file or read a file from that directory. To do so, Python has introduced this facility. You may not need this now, but Python Directory may help you later.

Python Create Rename Delete Directory Example

A simple program showing how to create directory, then rename and delete it.

When we execute above program through terminal, it produces following output.

Notice that os.rmdir can only remove empty directory. So to delete a non-empty directory, we will have to use shutil module. Below is a simple program to delete directory using shutil module.

So that’s all for python directory tutorial. Hope that you have learned well. Try to implement functions that are not implemented here. That’s you task! If you face any problem regarding this, feel free to use the comment section.


By admin

Leave a Reply

%d bloggers like this: