In part 4 of our Python OS series, we’re going to start creating and deleting files and folders so you can take full control of your system’s directory structures.

As with all the other tutorials in this series on the Python OS module, we’ll be using the following directory structure as our basis:

raw image

Our first tutorial left off with our current working directory set to C:\main directory, which you can set as your current working directory with this script:

import os
os.chdir(r"C:\main directory")
wd = os.getcwd()

Creating and Removing Files and Folders

Create Folders with Python OS

To create a folder or a directory with Python, use the mkdir() method. You simply need to pass the folder name to the method. The following script creates a folder named games() in your current working directory. Next, you can use the listdir() method to print a list of files and folders in your current working directory.

Notice, if you do not pass any path to the listdir() method, it prints the contents of your current working directory.

import os
os.mkdir(r"games")

Now, you can use the listdir() method to print a list of files and folders in your current working directory. If you don’t pass a path to the listdir() method, it prints the contents of your current working directory.

print(os.listdir())

The output below shows that the folder games has been created.

['documents',
 'games',
 'my_textfile.txt',
 'my_textfile2.txt',
 'my_wordfile.docx',
 'pictures']

You can also pass the full path to the mkdir() method, but just know the method doesn’t act recursively. For example, in the following script, if TestFolder doesn’t exist, you’ll get a FileNotFoundError when Python tries to make the MyGames subfolder.

import os
os.mkdir(r"C:\TestFolder\MyGames")

Here’s how to get around this limitation. Since the mkdir() method can only create a folder at the top level inside another directory, you can instead use the makedirs() method to make multiple folders at deeper levels at the same time.

For instance, the following script will create a folder named games2 inside the current working directory, and inside the games2 folder, it creates another folder named new_directory.

import os
wd = os.getcwd()
os.makedirs(wd + r"/games2/new_directory")
print(os.listdir())

Output:

The output shows the newly created games2 folder in the list of files and folders.

['documents',
 'games',
 'games2',
 'my_textfile.txt',
 'my_textfile2.txt',
 'my_wordfile.docx',
 'pictures']

To see if the contents of the games2 sub-folder, run the following script.

print(os.listdir(r"games2"))

You can see the newly created folder new_directory inside games2 folder.

Output:

['new_directory']

Before we start making files, I just want to demonstrate that the makedirs method works to create recursive folders with full file paths, too. Using our failed example from earlier, you can run the following script to create a TestFolder with a MyGames subfolder by declaring the full file path, regardless of your current working directory.

import os
os.makedirs(r"C:\TestFolder\MyGames")

Want to do more with Python?

We've uncovered a lot more tricks to help you get the most out of Python. To take your Python programming to the next level, enter your email address below and we'll send you our most powerful Python tricks yet.

Yes, show me more free Python tips

Create Files with Python

Okay, let’s now create a file inside the games2/new_directory folder we just made. You don’t need the OS module for that - you can simply use the default open() method, like this.

file = open( r'games2/new_directory/myfile.txt', 'w+')
file.close()

Running the above script will create the myfile.txt file inside the games2/new_directory folder in our current working directory.

To see if your file was actually created, list the contents of your games2/new_directory folder.

import os
print(os.listdir(r"games2/new_directory"))

Output:

['myfile.txt']

Removing Files and Folders with Python OS

Before you remove a folder with Python, you need to remove the files inside it. Let’s try to remove the myfile.txt file from the games2/new_directory folder. You’ll want to use the remove() method for that, as shown below:

import os
os.remove(r'games2/new_directory/myfile.txt')
print(os.listdir(r"games2/new_directory"))

You’ll notice the myfile.txt file was removed and the games2/new_directory folder is now empty.

Output:

[]

Once the directory is empty, you can use the rmdir() method to delete the folder by passing the directory path to the rmdir() method. The following script removes the games directory from your current working directory and displays the contents of the current working directory.

import os
os.rmdir("games")
print(os.listdir())

The output below shows that the “games” folder was successfuly removed.

Output:

['documents',
 'games2',
 'my_textfile.txt',
 'my_textfile2.txt',
 'my_wordfile.docx',
 'pictures']

Similar to the mkdir method, the rmdir method will only remove the inner most directory if your path contains multiple folders. For instance, the following script will only remove the new_directory folder and not the outer games2 folder. To remove the games2 folder you’ll first have to remove the new_directory folder, like this:

import os
os.rmdir(r"games2/new_directory")
print(os.listdir())

Output:

['documents',
 'games2',
 'my_textfile.txt',
 'my_textfile2.txt',
 'my_wordfile.docx',
 'pictures']

Finally, the script below removes the “games2” folder from your current working directory.

import os
os.rmdir("games2")
print(os.listdir())

Output:

['documents',
 'my_textfile.txt',
 'my_textfile2.txt',
 'my_wordfile.docx',
 'pictures']

If you enjoyed this series on the Python OS module, be sure to subscribe using the form below for more Python tips and tricks.


Want to do more with Python?

We've uncovered a lot more tricks to help you get the most out of Python. To take your Python programming to the next level, enter your email address below and we'll send you our most powerful Python tricks yet.

Yes, show me more free Python tips