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 Python directory, 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.

Get Current Directory

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

import os
print(os.getcwd())

Current Working Directory output

/Users/suresh/Documents/Simple Python

Python Change Directory

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

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

Output of changing current directory

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

Directories and Files list

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

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

Create a New Directory

Python mkdir method creates a new folder or directory. If you want this directory 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())
Python Create Directory 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 directory using the chdir. Next, listing the files and folders inside that directory using listdir.

import os
print(os.getcwd())

os.mkdir('/Users/suresh/Documents/NewPython')
os.chdir('/Users/suresh/Documents')
print(os.listdir())
Python Create Directory 2

Rename a File in Python

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

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

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

Rename Directory

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

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

Delete a Python File

A remove file function is to remove files from a directory. This Python example uses this file inside the remove function to delete the CopyFile.txt file. First, we are listing out the files in the current directory. Next, we removed that particular file, and then printing the files in the directory. 

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

Delete Directory in Python

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

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