Python standard library OS module

Source: Internet
Author: User

(Mastering the OS module, you need to understand the operation of common commands under Linux or Unix-like systems)

os.name  Indicates the platform you are using. For example, for Windows, it is ‘nt‘ , and for Linux/unix users, it is ‘posix‘ .

os.getcwd() Gets the current working directory, which is the directory path of the current Python script work.

Os.mkdir (' path ') creates a new directory that is a string representing the path to the new directory.

Os.rmdir (' path ' ) removes the empty directory, path is a string that represents the path of the directory you want to delete. Equivalent to the $rmdir command.

Stat ( path ) looks for additional information about the file that the path points to, equivalent to the $ls-l command.

os.listdir( ‘.‘)  Returns all file and directory names under the specified directory. ' Represents the current path

os.remove( ‘path‘ )  Used to delete a file

Rename ( src, DST ) renames the file, SRC and DST are two paths, each representing the path before and after renaming.

chmod ( path, mode ) changes the permissions of the file that the path points to. Equivalent to the $chmod command.

Chown ( path, UID, GID ) changes the owner and owning group of the file that the path points to. Equivalent to the $chown command.

os.system()function to run the shell command.

Symlink ( src, DST ) creates a soft link for the file DST, src for the path of the soft link file. Equivalent to the $ln-s command.

os.path.split() The function returns the directory name and file name of a path.

>>> os.path.split (' ~/ETC/VIM/VIMRC ')
(' ~/etc/vim ', ' VIMRC ')

os.path.isfile() and os.path.isdir()  the function separately check whether the given path is a file or a directory.

Similarly, the os.path.existe() function is used to verify that the given path really exists

Shutil Bag

Copy (src, DST) copies files from Src to DST. Equivalent to the $CP command.

>>> Import Shutil
>>> shutil.copy (' aa.py ', ' bb.py ')

Move (src, DST) moves files from Src to DST. Equivalent to the $MV command.

Python standard library OS module

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.