Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Python Directory

by suresh

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())
Python Get Current Directory

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())
Python Change Directory

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

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File Handling
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy