Summary of common functions of the Python OS Module

Source: Internet
Author: User

The advantages of the Python programming language are outstanding, and the advantages of these functions have attracted the attention of developers. In the term "Python", there is a standard module called the OS module. The Python OS module contains common operating system functions. This module is especially important if you want your program to be unrelated to the platform. That is, it allows a program to run in Linux and Windows without any changes or any problems.

  • A variety of common Python dictionary Application Methods
  • Python PDB simple debugging method
  • Summary of common Python Image Processing Techniques
  • Interpretation of the basic application of Python Regular Expressions
  • Interpretation of Python compressed file basic application code example

The following lists some useful parts of the Python OS module. Most of them are simple and clear.

OS. sep can replace the path delimiter specified by the operating system.

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.

The OS. getenv () and OS. putenv () functions are used to read and set environment variables respectively.

OS. listdir () returns all files and directory names in the specified directory.

The OS. remove () function is used to delete an object.

The OS. system () 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 () 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. existe () function is used to check whether the GIVEN path actually exists.

OS and OS. path modules

OS. listdir (dirname): List directories and files under dirname

OS. getcwd (): Get the current working directory

OS. curdir: returned but the former directory '.')

OS. chdir (dirname): change the working directory to dirname

OS. path. isdir (name): determines whether the name is a directory. If the name is not a directory, false is returned.

OS. path. isfile (name): determines whether the name is a file. If the name does not exist, false is returned.

OS. path. exists (name): determines whether a file or directory name exists.

OS. path. getsize (name): Get the file size. If the name is a directory, return 0L

OS. path. abspath (name): Obtain the absolute path.

OS. path. normpath (path): standard path string format

OS. path. split (name): split the file name and directory. In fact, if you fully use the directory, it will also separate the last directory as the file name, and it will not determine whether the file or directory exists)

OS. path. splitext (): separates 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): Return file path

The above are some important things of the Python OS module.

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.