Python File and Directory Management

In this tutorial we learn how we manage the file and Directory using the Python Programming language ,Python Provides in the Library os for managing the file and directory.

Python Directory

What is Directory ?

Basically Directory is a folder which contains the files.We can use Python Programming to manage the directory via using the simple code.

Let’s see the some basic code of python to manage the directory

  1. Get Current Directory
  2. Python 3.8.3 (default, Jul  2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.getcwd()
    'C:\\Users\\ASUS'
    >>> 
  3. Changing the Directory
  4. Python 3.8.3 (default, Jul  2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.getcwd()
    'C:\\Users\\ASUS'
    >>> os.chdir('C:\\Users')
    >>> os.getcwd()
    'C:\\Users'
    >>>  
    
  5. List of file in Directory
  6. >>> os.getcwd()
    'C:\\Users'
    >>> os.listdir()
    ['All Users', 'ASUS', 'Default', 'Default User', 'defaultuser100000', 'desktop.ini', 'Public']
    >>> 
    
    >>> os.listdir('D:\\')
    ['$RECYCLE.BIN', 'Images', 'Music', 'New folder', 'Software', 'System Volume Information', 'Video']
    >>>
    
  7. Create New Directory
  8. >>> import os
    >>> os.getcwd()     #know current directory
    'C:\\Users\\ASUS'
    >>> os.chdir("D:\\")     #changing directory
    >>> os.getcwd()
    'D:\\'
    >>> os.mkdir("salesforce")     #create new directory
    >>> os.listdir()
    ['$RECYCLE.BIN', 'Images', 'Music', 'New folder', 'salesforce', 'Software', 'System Volume Information', 'Video']
    >>>   
    
  9. Renaming Directory
  10. >>>import os
    >>> os.rename('salesforce','sfd')
    >>> os.listdir('D:\\')
    ['$RECYCLE.BIN', 'Images', 'Music', 'New folder', 'sfd', 'Software', 'System Volume Information', 'Video']
    >>>  
    
  11. Removing file and Directory
  12. >>> import os
    >>> os.chdir("C:\\Users\\ASUS\\Desktop\\")
    >>> os.getcwd()
    'C:\\Users\\ASUS\\Desktop'
    >>> os.listdir()
    ['desktop.ini', 'myproject', 'New folder', 'New folder (2)', 're.py', '__pycache__']
    >>> os.remove('re.py')
    >>> os.listdir()
    ['desktop.ini', 'myproject', 'New folder', 'New folder (2)', '__pycache__']
    >>> os.listdir()
    ['desktop.ini', 'myproject', 'New folder', 'New folder (2)', 'test', '__pycache__']
    >>> os.rmdir('test')
    >>> os.listdir()
    ['desktop.ini', 'myproject', 'New folder', 'New folder (2)', '__pycache__']
    >>>
    

    Note: rmdir() is used for only empty directory,if you want to delete non-empty directory so use rmtree()

scandir()

>>> import os
>>> path="/C:/Users/ASUS/"
>>> path
'/C:/Users/ASUS/'
>>> obj=os.scandir()
>>> for x in obj:
...     if(x.is_dir() or x.is_file()):
...             print(x.name)
...
.conda
.condarc
.idlerc
.ipynb_checkpoints
.ipython
.jupyter
.python_history
3D Objects
AppData
Application Data
build
Contacts
Cookies
curd
Desktop
dist
Documents
Downloads
Favorites
info.csv
IntelGraphicsProfiles
Links
Local Settings
MicrosoftEdgeBackups
Music
My Documents
myApp.spec
mypro
NetHood
OneDrive
Pictures
PrintHood
Recent
salesforcedrillers
Saved Games
Searches
Subscribe Now