Common uses of Python's OS modules

Source: Internet
Author: User

Includes common operating system features in Python's standard library OS module

The OS module introduces the following method: Import OS

The following are common usage methods for OS modules:

1, OS.GETCWD get the current working directory, that is, the program execution directory

2. The Os.name string indicates the platform being used. For example, Windows is "NT", Linux or UNIX is "POSIX"

3, os.getenv () Read environment variables. For example, os.getenv (' PATH ')

4, Os.setenv () set the environment variable.

5, Os.listdir () returns all the files and folders in the specified directory, and the returned format is list. such as Os.listdir ('/boot/'), returns the result:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4C/06/wKiom1Q2qJ-SFr0oAAF4d9rfTCQ866.jpg "title=" 0.png " alt= "Wkiom1q2qj-sfr0oaaf4d9rftcq866.jpg"/>

6, Os.remove () to delete a file

7. Os.system () function execute system command, Shell command under Linux or cmd command under Windows

8, Os.path.isfile () check whether the given path is a file

9, Os.path.isdir () check whether the given path is a folder

10, Os.path.existe () check whether the given path is real

11, Os.chdir (dirname) Change the current working directory to the specified dirname path

12. The Os.getpid () function gets the PID of the current process

13. The Os.getppid () function says that the PID of the parent process to the current process is only valid on UNIX or Linux systems

14, Os.rmdir () Delete an empty folder, must be empty folder

15, Os.remove () delete a file

16, Os.rename (old,new) Rename the folder or file.

17, Os.mkdir () Create a directory, Os.mkdirs recursively create a folder, when the folder exists is, will throw OSError exception

18, Os.chmod (path,mode) Change the permissions of the file or folder

Mode is available in the following categories:

  • Stat.                       S_isuid: Set user ID on execution. Not used

  • Stat.                    S_isgid: Set group ID on execution. Not used

  • Stat.                    S_ENFMT: Record locking enforced. Not used

  • Stat.           S_ISVTX: Save text image after execution. Save text and pictures after execution

  • Stat.                                     S_iread: Read by Owner. Permissions for the owner to read

  • Stat.                                   S_iwrite: Write by Owner. Permissions written for the owner

  • Stat.                                  S_iexec: Execute by Owner. Permissions performed on the owning person

  • Stat.     S_irwxu: Read, Write, and execute by owner. Permissions for owner read and write execution

  • Stat.                                      S_IRUSR: Read by Owner. Permissions for the owner to read

  • Stat.                                    S_IWUSR: Write by Owner. Permissions written for the owner

  • Stat.                                  S_IXUSR: Execute by Owner. Permissions performed on the owning person

  • Stat.      S_IRWXG: Read, Write, and execute by group. Permissions for read and write execution for people in the same group

  • Stat.                                      S_IRGRP: Read by group. Permissions for the same group read

  • Stat.                                    S_IWGRP: Write by group. Permissions for the same group write

  • Stat.                                  S_IXGRP: Execute by group. Permissions for the same group execution

  • Stat.     S_irwxo: Read, Write, and execute by others. Permissions for read and write execution of other groups

  • Stat.                                     S_iroth: Read by others. Permissions for other groups to read

  • Stat.                                   S_iwoth: Write by others. Permissions written for other groups

  • Stat.                                 S_ixoth: Execute by others. Permissions performed on other groups

To be continued ....

This article is from the "mask_x blog" blog, please be sure to keep this source http://zhpfbk.blog.51cto.com/4757027/1561926

Common uses of Python's OS modules

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.