Collection of practical methods for the OS and shutil modules in Python

Source: Internet
Author: User

Copy codeThe Code is as follows:
# OS Module

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'
The OS. getcwd () function obtains the current working directory, that is, the directory path of the current Python script.
OS. getenv () gets an environment variable. If none is not returned
OS. putenv (key, value) sets an environment variable value
OS. listdir (path) returns all files and directory names in the specified directory.
The OS. remove (path) function is used to delete an object.
The OS. system (command) function is used to run shell commands.
The OS. linesep string specifies 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' is used'
The OS. path. split (path) function returns the directory name and file name of a path.
The OS. path. isfile () and OS. path. isdir () functions verify whether the given path is a file or a directory.
The OS. path. exists () function is used to check whether the GIVEN path actually exists.
OS. curdir: returns the current directory ('.')
OS. mkdir (path) to create a directory
OS. makedirs (path) recursive Directory Creation
OS. chdir (dirname) changes the working directory to dirname
OS. path. getsize (name) to get the file size. If the name is a directory, return 0L
OS. path. abspath (name) to obtain the absolute path
OS. path. normpath (path) standard path string format
OS. path. splitext () Separation of file names and extensions
OS. path. join (path, name) connection directory and 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 iteration 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. Like rename ()

# Shutil Module

Copy shutil. copyfile (src, dst) 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, it will be overwritten.
Shutil. move (src, dst) move or rename the file
Shutil. copymode (src, dst) will only copy its permissions. Other things will not be copied.
Shutil. copystat (src, dst) replication permission, last access time, last modification time
Shutil. copy (src, dst) copies an object to a file or directory.
Shutil. copy2 (src, dst) copies the last access time and modification time of the file based on the copy, similar to cp-p.
Shutil. copy2 (src, dst) if the file system in the two locations is 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)
Copy the olddir file to newdir. If the value of the 3rd parameter is True, the symbolic connection under the folder will be maintained during directory replication. If the value of the 3rd parameter is False, A physical copy is generated under the copied directory to replace the symbolic connection.
Shutil. rmtree (src) recursively deletes a directory and all contents in the directory.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.