Common module commands in Python (os/sys/platform)

Source: Internet
Author: User


First, Os

The OS modules in the Python standard library are primarily concerned with common operating system functions. Can run under Linux and Windows, regardless of platform.

OS.SEP can replace operating system-specific path separators.

The Os.name string indicates the platform you are using. For example, for Windows, it's ' NT ', and for Linux/unix users, it's ' POSIX '.

The OS.GETCWD () function gets the current working directory, which is the directory path of the current Python script work.

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

Os.listdir () returns all file and directory names under the specified directory.

The Os.remove () function is used to delete a file.

The Os.system () function is used to run the shell command.

The OS.LINESEP string gives the line terminator used by the current platform. For example, Windows uses ' \ r \ n ', Linux uses ' \ n ' and Mac uses ' \ R '.

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

The Os.path.isfile () and Os.path.isdir () functions respectively verify that the given path is a file or a directory.

The Os.path.existe () function is used to verify that the given path really exists

OS and Os.path modules

Os.listdir (dirname): List directories and files under DirName

OS.GETCWD (): Get current working directory

Os.curdir: Return but previous directory ('. ')

Os.chdir (dirname): Changing the working directory to DirName

Os.path.isdir (name): Determine if Name is a directory, name is not a directory and return false

Os.path.isfile (name): Determine if name is not a file, does not exist name also returns false

Os.path.exists (name): Determine if there is a file or directory name

Os.path.getsize (name): Get file size, if name is directory return 0L

Os.path.abspath (name): Get absolute path

Os.path.normpath (PATH): Canonical path string form

Os.path.split (name): Split file name and directory (in fact, if you use the directory completely, 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 (): Detach file name and extension

Os.path.join (path,name): Connection directory with file name or directory

Os.path.basename (PATH): Return file name

Os.path.dirname (PATH): Return file path


Second, Sys

SYS.ARGV: Implements passing parameters from outside the program to the program.

Sys.exit ([arg]): Exit in the middle of the program, arg=0 for normal exit.

Sys.getdefaultencoding (): Gets the system current encoding, which is generally ASCII by default.

Sys.setdefaultencoding (): Set system default encoding, do not see this method when executing dir (SYS), do not pass in interpreter, can execute reload (SYS), execute setdefaultencoding (' UTF8 ' ), the system default encoding is set to UTF8. (See Set system default encoding)

Sys.getfilesystemencoding (): Gets the file system using encoding, returns ' MBCS ' under Windows, and returns ' Utf-8 ' under Mac.

Sys.path: Gets a collection of strings for the specified module search path, which can be placed in a given path, and can be found correctly when import is in the program.

Sys.platform: Gets the current system platform.

Sys.stdin,sys.stdout,sys.stderr stdin, stdout, and stderr variables contain stream objects that correspond to standard I/O streams. If you need more control over the output, and print does not meet your requirements, they are what you need. You can also replace them, so you can redirect output and input to other devices, or handle them in a non-standard way

Third, Paltform

Platform.system () Get OS type, Windows, Linux, etc.

Platform.platform () Get the operating system, Darwin-9.8.0-i386-32bit

Platform.version () Get System version information 6.2.0

Platform.mac_ver ()

Platform.win32_ver (' Post2008server ', ' 6.2.9200 ', ', U ' multiprocessor free ')

............

This article from "Flat Light is true" blog, please be sure to keep this source http://ucode.blog.51cto.com/10837891/1870977

Common module commands in Python (os/sys/platform)

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.