Untitled Document
OS Module
1.Introduction Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Introduction 
       
       The OS Module is used for crating files , directories moving from direcctories ( i.e changing path ) . 
       Printing list of files from a directory .
       
       Getting file information like modification time , access time , size of the file , file name etc . 
       
       The example covers different methods available in OS module which are frequently used in Pythong Programming
       
      
2. Program - To Display list of Methods available in OS Module Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To Display list of Methods available in OS Module
       
       import os
       print(dir(os))
       
3. How to get path of current working directory Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program -How to get path of current working direectory  
       # Here we will use os.getcwd() method
       
        import os 
        print(os.getcwd()) 
       
4. How to change the directory Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - How to change the directory 
       # chdir('path_name') function is used to change the directory
       
       import os 
       print(os.getcwd())
       os.chdir('I:\Devops-2021')
       print(os.getcwd())
       
5. How to get list of files and directories from current path Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # How to get list of files and directories from current path
       # Note - you can get list of files and directories from any location 
       # only thing is you need to switch to that specific directory 
       
        import os 
        print(os.listdir())             

       
6. How to create Directory and Directories Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # How to create Directory and Directories
       # In python we have 2 methods mkdir and makedirs
       # mkdir creates only one directory and makedirs create directores and subdirectories i.e directory structure 
       
       import os
       os.chdir('I:\Devops-2021\Selenium+Python')
       os.makedirs('Demo-1\Sub-Dir1')            
       os.mkdir('Demo-2')
 
       
7. Removing Directory and Directories Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Removing Directory and Directories
       # rmdir will only single given directory & removedirs will remove the directory and subdiretories provide in path 
       
       import os 
       os.chdir('I:\Devops-2021\Selenium+Python')            
       os.rmdir('Demo-2')
       os.removedirs('Demo-1\Sub-Dir1')
 
       
8. Renaming file and directory Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Renaming file and directory
     
       import os
       os.chdir('I:\Devops-2021\Selenium+Python\Demo-2')
       print(os.listdir())
       # Renaming File
       os.rename('test.txt','demo.txt')
       print(os.listdir())    
       # Renamin Directory         
       os.chdir('I:\Devops-2021\Selenium+Python')
       os.rename('Demo-2','Demo-21')
 
       
9. How to get information about file Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # How to get information about file
       # In below example we are printing size of the file 
       
       import os
       os.chdir('I:\Devops-2021\Selenium+Python\Demo-21')
       print(os.stat('demo.txt'))  
       # printing size of the file 
       print(os.stat('demo.txt').st_size)          
       
       #Approach - 2  ( using .format method)
       
        str1 = "Hello" 
        str2 = "User !!"
       print("{} {} . Welcome" .format(str1,str2))
 
       
10. How to print modifcation time of a file Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # How to print modifcation time of a file

       import os
       from datetime import datetime
       os.chdir('I:\Devops-2021\Selenium+Python\Demo-21')  
       mod_time = os.stat('demo.txt').st_mtime
       print(datetime.fromtimestamp(mod_time))
       
       
11. How to traverse through complete directory structure Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # How to traverse through complete directory structure
       # printing complete directory structure can be done by os.walk()
       # it will first print all the directrories and files from the givien path 
       # then it will print sub- directories and files inside the subdirectories 
       # the series continues till it rach to end of tree structure 
       # os.walk gives three varibales tuple whiich contains directory path, direcories names , File names
       
       import os    
       os.chdir('I:\Devops-2021\Selenium+Python') 
       for dirpath, dirname, filenames in os.walk('I:\Devops-2021\Selenium+Python'):
       	print('current path:', dirpath)
	   	print('Directories:',dirname)
	   	print('Files:', filenames)
	   	print()     
       
       
12. How to get list of Environment variables Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # How to get list of Environment variables
       # Example - 1
       # the below code will print list of all environment varibales sometimes the list may be huge
       
       import os    
       print(os.environ) 
       
       # Example - 2  to get details about specific Environment variables we can use get method
       # The below code will print details about HOME Environment varibales 

       import os    
       print(os.environ.get('HOME')) 
       
       
13. Creating dynamic file path ( Using Environment varibale ) Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Creating dynamic file path ( Using Environment varibale )
       # here we have useed os.path.join method - just to avoid the confusion about last \ in the path
       # sometimes these \ crates problem if it is missing ; so os.path.join takes care of it
       
       import os    
       file_path=os.path.join(os.environ.get('HOME'), 'test.txt')
       print(file_path) 
       
       # Example - 2  to get details about specific Environment variables we can use get method
       # The below code will print details about HOME Environment varibales 

       import os    
       print(os.environ.get('HOME')) 
       
       
14. some other important methods in os.path Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # some other important methods in os.path
       # Example - 1 how to get filename from path 
       
       import os    
       print(os.path.basename('/tmp/mangesh/test.txt')) 
       
       # Example - 2  getting dirname from above path 
       
       import os    
       print(os.path.dirname('/tmp/mangesh/test.txt')) 
       
       # Example - 3 getting file path and name 
       # if you want to get filename and path (  both ) then use split command 

       import os    
       print(os.path.split('/tmp/mangesh/test.txt')) 
       
       # Example - 4 How to check if the path exists on file system  
       
       import os    
       print(os.path.exists('I:\Devops-2021\Selenium+Python'))
       
       # Example - 5 How to check whether given path is directory or afile 
       # sometime we get files where we don't ahve extensions and such file are ahrd to recognise whether its directory or file 
       # Here are two examples which will demo on file and dir 
       
       import os    
       print(os.path.isfile('/tmp/abcdef')) 
       print(os.path.isdir('I:\Devops-2021\Selenium+Python'))
       
       # Example - 6 How to get filename with full path and extension of file separatel 
       
       import os    
       print(os.path.splitext('tmp/test.txt')) 
              
       # Example - 7 How to get list of all the methods available in os.path  module run below command
       
       import os
       print(dir(os.path))
       
Untitled Document