A collection of practical methods for OS and shutil modules in Python

  • 2020-04-02 13:38:08
  • OfStack


# os  The module 

os.sep  Can replace OS - specific path separators. windows for  '\'
os.name  The string indicates the platform you are using. Such as for Windows , it is 'nt' , and for Linux/Unix User, it is  'posix'
os.getcwd()  The current working directory () function gets the current working directory Python Directory path for script work 
os.getenv()  Gets an environment variable if none is returned none
os.putenv(key, value)  Set an environment variable value 
os.listdir(path)  Returns all files and directory names in the specified directory 
os.remove(path)  The deletes () function deletes a file 
os.system(command)  Function to run shell The command 
os.linesep  String gives the line terminator used by the current platform. For example, Windows use  'rn' . Linux use  'n'  while Mac use  'r'
os.path.split(path)   Function returns the directory name and file name of a path 
os.path.isfile()  and os.path.isdir() The function checks whether the given path is a file or a directory 
os.path.exists()  Function to verify that the given path exists 
os.curdir   Return to current directory  ('.')
os.mkdir(path)  Create a directory 
os.makedirs(path)  Recursively create directories 
os.chdir(dirname)  Change the working directory to dirname    
os.path.getsize(name)  Get the file size if name Directory return 0L
os.path.abspath(name)  Get the absolute path 
os.path.normpath(path)  specification path String form 
os.path.splitext()   Separate file name and extension 
os.path.join(path,name)  Connect directories to file names or directories 
os.path.basename(path)  Return file name 
os.path.dirname(path)  Return file path 
os.walk(top,topdown=True,onerror=None)   Iterate through the iterated directory 
os.rename(src, dst)   rename file or directory src to dst  if dst There is one directory,  Will throw OSError.  in Unix,  if dst Is in existence and is a file,  If the user has permission, it will be replaced quietly .  The operation will fail at some point Unix  If in the src and dst In different file systems .  If successful ,  The naming operation will be an atomic operation  ( This is a POSIX  Need to be ).  in  Windows on ,  if dst existing ,  Will throw OSError Even if it is a file .  in unix . Windows The effective. 
os.renames(old, new)  Recursively rename folders or files. like rename()
# shutil  The module 
shutil.copyfile( src, dst)  From the source src Copied to the dst . The premise of course is that the destination address has writable permissions. The exception information thrown is IOException.  If the current dst If it exists, it will be overwritten 
shutil.move( src, dst)   Move a file or rename it 
shutil.copymode( src, dst)  It just copies its permissions and nothing else is copied 
shutil.copystat( src, dst)  Copy permissions, last access time, last modification time 
shutil.copy( src, dst)   Copy a file to a file or directory 
shutil.copy2( src, dst)   in copy The last access time and the modification time of the file are also copied, similar to cp  � p The things 
shutil.copy2( src, dst)   If the file systems in both locations are the same, it's equivalent rename Operation, just rename; If it's not on the same file system move operation 
shutil.copytree( olddir, newdir, True/Flase)
 the olddir Copy a newdir If the first 3 A parameter is True , the symbolic connection under the folder will be kept when copying the directory, if no 3 A parameter is False , a physical copy is generated under the copied directory to replace the symbolic join 
shutil.rmtree( src )  Recursively deletes a directory and everything in it 


Related articles: