Python Directory

While working with files, you should know which Python directory you are using, where your files are storing so on. If you know them, you can easily change the working directories or even create subfolders to organize your files.

In Python, we have an os module, which contains all the necessary functions to work with file directories. So, to work with the methods, you have to import this os module.

Python Directory Examples

The following list of examples helps you to understand the available functions, which helps you to work with Python Directory. They are getcwd, chdir, listdir, mkdir, rmdir, and rename function.

How to Get a Current Directory?

Python getcwd method returns the current working directory. This getcwd function example shows you the same.

import os
print(os.getcwd())
/Users/suresh/Documents/Simple Python

The getcwd method helps you to change the current folder to a new location.

import os
print(os.getcwd())
 
os.chdir('/Users/suresh/Documents')
print(os.getcwd())

Output of changing current dir

/Users/suresh/Documents/Simple Python
/Users/suresh/Documents

Python Directory and Files list

The listdir method returns all the files and the subfolders available inside that folder.

import os
print(os.getcwd())
 
print(os.listdir())
Python Directories and Files list

Create a New Directory in Python

The mkdir method creates a new folder. If you want this dir inside the current working folder, then simply specify the folder name; otherwise, specify the full path.

import os
print(os.getcwd())
 
os.mkdir('NewPython')
print(os.listdir())
Create a New Folder using os mkdir 1

Let me provide the full path so that I can create a folder in a different location. Here, we first created a folder. Next, we changed the current working folder using the chdir method. Next, listing the documents and folders inside that it using listdir.

import os
print(os.getcwd())

os.mkdir('/Users/suresh/Documents/NewPython')
os.chdir('/Users/suresh/Documents')
print(os.listdir())
Create a New Folder by passing Full path to mkdir and chdir 2

Rename a Directory

The rename function present in the os module helps us to rename existing files or even renaming folders. Here, we use this rename file function to rename PythonSampleCopy to the Sample1 text.

import os
print(os.getcwd())
 
print(os.listdir())

os.rename("PythonSampleCopy.txt", "Sample1.txt")
print(os.listdir())
Python Rename File

The rename function that we mentioned above also renames the Python directory. Let us use this rename folder function to rename FirstFolder to SecondFolder.

import os
print(os.getcwd())
 
print(os.listdir())
 
os.rename("FirstFolder", "SecondFolder")
print(os.listdir())
Python Rename Directory

Delete a File

A remove function is to remove files from a folder. This example uses a sample inside the remove function to delete the CopyFile.txt.

First, we are listing out the files in the current working folder. Next, we removed that particular one, and then printing the files.

import os
print(os.getcwd())
 
print(os.listdir())
 
os.remove("CopyFiles.txt")
print(os.listdir())
Python Delete File

The rmdir function is to delete a Python directory. Here, we used the rmdir function to delete the existing SecondFolder.

import os
print(os.getcwd())
 
print(os.listdir())
 
os.rmdir("SecondFolder")
print(os.listdir())
Python Delete Directory