Python OS MODULE Current Working Directory














































Python OS MODULE Current Working Directory




            Handling Current Working Directory


                   We can consider Current Working Directory  (CWD ) as a folder, Whenever the files are called only by their name, Python operating system (here python work as an operating system) assumes that is starts in the CWD which means that name-only reference will be successful only if the file is in the python's CWD.

Note:-
The folder where the python script is running is known as the Current Directory.


1. Getting Current Working Directory

To get the location of the current working directory os.getcwd() is used.

Example:-



Output:-

 C:UsershpOneDriveDocumentsDesktopd


2. Changing the Current Working Directory 


For changing the current working directory(CWD) os.chdir() function is used.This function change the CWD to a specified path. It only takes a one  argument as a new directory path.


Example:-



 Output:-

Current Working Directory: C:\Users\hp\OneDrive\Documents\Desktop\
After Changing CWD : C:\Users\hp\OneDrive\Documents


3. Getting the list of all files & directory

                        os.listdir() function is used to get the list of all files and directoies in the specified directory. If we don't specify any directory, then the list of files and directories in the current working directory will be returned.

Example:-




Output:-

['.ipynb_checkpoints','.python.py.swp', 'Adobe ReaderXI.lnk','bank.db',
'bank_data.txt','data','hurdle_task.py','InternshipTask.ipynb',
'log.txt', 'Pandas_Exercises.ipynb', 'Python file', 'PYTHON WEB 
SCRAPPING.ipynb', 'shobhs.ipynb', 'testpaper.ipynb',
'Untitled.ipynb', 'Untitled1.ipynb', 'Untitled2.ipynb', 'vanshu.py','Webscrapping.ipynb']

4. Creating a Directory


There are different methods available in the OS module for creating a directory. These are:-

  • os.mkdir()                                                                                                                                        
                   os.mkdir() function is used to create a directory named path with the specified numeric mode. This function raise FileExistsError if the directory which has been created is already exists.
Example:-



Output:-

['25thMay2021.docx','com.moodle.moodlemobile','Demo','Desktop',

'desktop.ini']

      
  • os.makedir()
                    os.makedirs() function is used to create a directory monotonously. That means while making leaf directory if any intermediate-level directory is missing, os.makedirs() method will create them all.   
Example:-



 Output:-


['.ipynb_checkpoints','.python.py.swp', 'Adobe Reader XI.lnk', 'bank.db','data','hurdle_task.py','log.txt','module1','Pandas_Exercises.ipynb',
'Python file', 'Untitled2.ipynb']


Note:- Here we have created nested directories.That means in module1 there is another

directory module2.


5. Deleting Dictionary or files

 os module proves different methods for removing directories and files in Python. These are


  • Using os.rmdir()
      os.rmdir() function is used to remove or delete a empty directory. OSError will be raised if the specified path is not an empty directory.

 Example:


 


Output:-


['25th May 2021.docx',  'com.moodle.moodlemobile', 'Desktop', 'desktop.ini']


  • Using os.removedirs()

          os.removedirs() function is used to remove or delete nested directory.


 Example:

 

Output:-

['.ipynb_checkpoints',
'.python.py.swp', 'Adobe Reader XI.lnk', 'bank.db', 'data',
'hurdle_task.py',log.txt','Pandas_Exercises.ipynb', 'Python file',
'Untitled2.ipynb']



Next Article:

More Articles of shobha dhande:

Name Views Likes
Python OS.PATH Module os.path.normcase() Method 370 0
Python OS.PATH Module os.path.exists() Method 593 1
Python OS.PATH MODULE os.path.lexists() Method 296 1
Python OS.PATH MODULE os.path.samestat() Method 326 1
Python OS.PATH MODULE os.path.getsize() Method 402 1
Python OS..PATH MODULE os.path.getctime() Method 386 1
Python OS.PATH MODULE os.path.getmtime() Method 482 1
Python OS.PATH MODULE os.path.getatime() Method 313 1
Python OS.PATH MODULE os.path.splitdrive() Method 400 0
Python OS.PATH MODULE os.path.splitext() Method 393 0
Python OS.PATH MODULE os.path.split() Method 356 0
Python OS.PATH MODULE os.path.sameopenfile() Method 288 0
Python OS.PATH MODULE os.path.samefile() Method 365 0
Python OS.PATH MODULE os.path.relpath() Method 335 0
Python OS.PATH MODULE os.path.realpath() Method 491 0
Python OS.PATH MODULE os.path.join(path, *paths) Method 352 0
Python OS.PATH MODULE os.path.normpath() Method 432 0
Python OS.PATH MODULE os.path.ismount(path) Method 293 0
Python OS.PATH MODULE os.path.link() Method 325 0
Python OS.PATH MODULE os.path.isfile(path) Method 293 0
Python OS.PATH MODULE os.path.isdir() Method 515 0
Python OS.PATH MODULE os.path.dirname() Method 354 0
Python OS.PATH MODULE os.path.isabs() Method 275 0
Python OS.PATH MODULE os.path.commonprefix() Method 304 0
Python OS.PATH MODULE os.path.commonpath() Method 294 0
Python OS.PATH MODULE os.path.basename() Method 297 0
Python OS.PATH MODULE os.path.abspath() Method 488 0
Python OS MODULE os.access() Method 353 0
Python OS MODULE os.get_terminal_size() Method 410 0
Python OS MODULE os.umask() Method 402 1
Python OS MODULE os.DirEntry.stat() Method 381 1
Python OS MODULE os.DirEntry.path Attribute 416 1
Python OS MODULE os.DirEntry.name Attribute 296 1
Python OS MODULE os.DirEntry.is_symlink() Method 287 1
Python OS MODULE os.DirEntry.is_dir() Method 468 1
Python OS MODULE os.scandir() Method 443 1
Python OS MODULE os.environ 711 1
Python OS MODULE Introduction to OS Module 311 1
Python OS MODULE os.system() Method 325 1
Python OS MODULE os.getpid() 344 1
Python OS MODULE os.getlogin() 356 1
Python OS MODULE os.getgroups() Method 310 1
Python OS Module os.getgrouplist() Method 331 1
Python OS MODULE os.getuid() and os.setuid() Method 754 1
Python OS MODULE os.getgid() and os.setgid() Method 346 1
Python OS MODULE os.geteuid() and os.seteuid() Method 466 1
Python OS MODULE os.getegid() and os.setegid() Method 314 1
Python OS MODULE Difference between Real, Effective and Saved UserID 303 1
Python OS MODULE Current Working Directory 355 1
Python OS MODULE : os.getenvb() , os.get_exec_path() 471 1

Comments