os common function module

This article describes common features of python os module comprising:

  • New, delete the directory
  • Delete Files
  • Gets the absolute path
  • Split path
  • File size acquisition
  • How python script calls linux command
  • Determine whether a file, directory
  • Determine whether a file exists
  • # os.chdir (), switching paths 
    os.chdir ( "../") 
    
    # os.getcwd, output current path 
    os.getcwd () 
    
    # os.mkdir (), to create a test directory 
    os.mkdir ( "test") 
    
    # os.rmdir (), delete haha directory 
    os.rmdir ( "./ haha") 
    
    # os.remove, delete a file 11 
    os.remove ( "./ 11") 
    
    # os.system, Python script calls a shell command 
    os.system ( "mkdir haha") # call the shell mkdir to create a directory haha 
    
    
    # os.listdir (), all files and directories under the output path, returns a list 
     os.listdir ( "./") 
     
     # os.path.split (path), the path is divided into path and file name or directory name 
    the os.path.split ( '/ Home / MX / Study / Test') 
    # returns a value tuple, ( '/ home / mx / study', ' Test ') 
    The os.path.split (' / Home / MX / Study / Test ') [0] # path name,' / Home / MX / Study ' 
    The os.path.split (' / Home / MX / Study / Test ') [1] # file or directory name,Test 
    
    # the os.path.join (), the connection path and the file or directory
    the os.path.join ( 'Home / MX / Study', 'Test') # '/ Home / MX / Study / Test'
    
    
    os.path.basename () # Returns the file name 
    os.path.dirname () # returns the file path 
    
    
    # os.path.isfile (), to determine whether a given file is a file and returns True or False 
    os.path.isfile ( "test.bam") 
    # os.path.isdir (), to determine whether a given file is a directory, returns True or False 
    os.path.isdir ( "HAH") 
    
    
    # os.path.exists (), to determine whether there file or directory, returns True or False 
    os.path.exists ( "test.bam") 
    
    # os.path.getsize, get the file size, if it is a directory to return 0L 
    
    # os.path.abspath (), obtain an absolute file path 
    os .path.abspath ( "test.bam") # ' / home / mx / study / test / test.bam'
    

      

Guess you like

Origin www.cnblogs.com/mydx/p/11681719.html