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 this Python programming language, 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 Python examples helps you to understand the available functions, which helps you to work with Directory. They are getcwd, chdir, listdir, mkdir, rmdir, and rename function.

Get a Current Directory using getcwd

The 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())

The output of changing current dir

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

Python Directory and Files list using listdir

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

import os
print(os.getcwd())
 
print(os.listdir())
Folder and Files list

Create a New Directory using mkdir

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 or Directory using os mkdir in Python 1

Change the Current Directory using Python chdir

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, list 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 or directory by passing Full path to mkdir and chdir in Python 2

Rename a Directory in Python

The rename function present in the os module helps us to rename existing files or even rename 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())
Rename File

The rename function that we mentioned above also renames the 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 using the remove function

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 printed the files.

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

Use rmdir to delete the Directory

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())
Delete folder