Python default module OS and shutil utility functions, pythonshutil
OS. sep can replace the path separator specified by the operating system. In windows, the '\' OS. name string indicates the platform you are using. For example, for Windows, it is 'nt ', and for Linux/Unix users, it is 'posix' OS. the getcwd () function obtains the current working directory, that is, the operating directory path of the current Python script. getenv () gets an environment variable. If no noneos is returned. putenv (key, value) sets an environment variable value OS. listdir (path) returns all files and directory names in the specified directory OS. the remove (path) function is used to delete a file OS. the system (command) function is used to run the shell command OS. linesep string indicates the row Terminator used by the current platform. For example, in Windows, '\ r \ n' is used, in Linux,' \ n' is used, and in Mac, '\ R' OS is used. path. the split (path) function returns the directory name and file name of a path, OS. path. isfile () and OS. path. the isdir () function checks whether the given path is a file or a directory OS. path. the exists () function is used to check whether the given path actually exists in the OS. curdir returns the current directory ('. ') OS. mkdir (path) creates a directory OS. makedirs (path) recursively creates the directory OS. chdir (dirname) changes the working directory to dirname OS. path. getsize (name) is used to obtain the file size. If the name is a directory, return 0Los. path. abspath (name) obtains the absolute path OS. path. normpath (path) specifications path string form OS. path. splitext () separates file names and extensions o S. path. join (path, name) connects the directory to the file name or directory OS. path. basename (path) returns the file name OS. path. dirname (path) returns the file path OS. walk (top, topdown = True, onerror = None) traverses the iterative directory OS. rename (src, dst) rename file or directory src to dst if dst is an existing directory, OSError will be thrown. in Unix, if dst is stored and is a file, it will be quietly replaced if the user has the permission. the Operation will fail in Some Unix systems if src and dst are in different file systems. if successful, the naming operation will be an atomic operation (required by POSIX ). in Windows, if dst already exists, OSError will be thrown, even if it is a file. valid in unix and Windows. OS. renames (old, new) recursively rename folders or files. For example, rename () # shutil module shutil. copyfile (src, dst) is copied from the source src to dst. Of course, the premise is that the target address has the write permission. IOException is thrown. if the current dst already exists, the shutil will be overwritten. move (src, dst) move the file or rename shutil. copymode (src, dst) only copies its permissions. Other things are not copied. copystat (src, dst) replication permission, last access time, last modification time shutil. copy (src, dst) copies a file to a file or directory. copy2 (src, dst) copies the last access time and modification time of the file based on the copy, similar to the cp-p thing shutil. copy2 (src, dst) if the file systems in the two locations are the same, it is equivalent to the rename operation, but it is renamed. If it is not the same file system, it is the move operation shutil. copytree (olddir, newdir, True/Flase) Copies olddir to newdir. If the 3rd parameters are True, the symbolic connections in the folder are maintained during directory replication, if the first parameter is False, a physical copy is generated under the copied directory to replace the shutil. rmtree (src) recursively deletes a directory and all contents in the directory.
Python function Parsing
Lambda is an anonymous function. Here, the dictionary {True: f1, False f2} is generated. The two values of the dictionary are two functions, and f1 is shutil. copy (...), f2 is the copytree. You can understand it as two function pointers.
Bresult is a value of true or false, followed by [bresult] (): It indicates that two functions corresponding to the previous dictionary are called according to bresult results. Bresult is the dictionary key here. The function is a dictionary value here.
Differences between python OS and ospath modules
OS includes OS. path
After the OS is imported, OS. path is called.
From OS import path can be directly called using path.