Python Beginner's sys, OS, Platform Module __python

Source: Internet
Author: User
Tags exit in stdin python script

The SYS module can obtain command-line parameters, implement the function of passing parameters from outside the program to the program, and obtain the program path and the information of the current platform.

1.sys.plateform Current operating system platform

2. Command line arguments

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

#!usr/bin/python
#coding: UTF8
import sys
print u ' total%d command-line arguments '% (SYS.ARGV) for

i in range (Len ( SYS.ARGV)):
    print U ' first ', i + 1, u ' parameters are ', Sys.argv[i]
Enter Python xxx.py a b c on the command line

3.sys.getdefaultencoding () Gets the current system encoding


4.sys.exit ([arg]): Exit in the middle of the program, arg=0 for normal exit. arg = 1 o'clock has an error exiting.

5.sys.setdefaultencoding (): Sets the system default encoding, does not see this method when executing dir (SYS), executes in the interpreter does not pass, can execute reload (SYS) First, executes setdefaultencoding (' UTF8 '), set the system default encoding to UTF8. (See Set system default encoding)

6. Sys.getfilesystemencoding (): Gets the file system to use the encoding method, Windows returns ' MBCS ', the Mac returns ' Utf-8 '. Sys.path: Gets a collection of strings for the specified module search path, where the written module can be placed under one of the resulting paths and can be found correctly in the import of the program.

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

For more information, see Click on the Open link

Two. Plateform

Platform.platform () #获取操作系统名称及版本号

Platform.version () #获取操作系统版本号

Platform.architecture () #获取操作系统的位数

Platform.machine () #计算机类型

Platform.node () #计算机的网络名称 '

Platform.processor () #计算机处理器信息 '

Platform.uname () #包含上面所有的信息汇总

You can also get some information about Python in your computer: Platform.python_build () Platform.python_compiler () Platform.python_branch () Platform.python_implementation () platform.python_revision () platform.python_version () PLATFORM.P Ython_version_tuple ()

Third, Os

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

OS.SEP can replace the operating system-specific path delimiter.

The Os.name string indicates the platform you are using. For example, for Windows, it is ' 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 the file and directory names in the specified directory.

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

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

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 Verify that the path given is either 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): Listing directories and files under DirName

OS.GETCWD (): Get current working directory

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

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

Os.path.isdir (name): Determines if name is a directory, name is not a directory returns false

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

Os.path.exists (name): Determines whether a file or directory name exists

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 filename, and it will not determine whether the file or directory exists)

Os.path.splitext (): Detach file name and extension

Os.path.join (path,name): Connecting directories with file names or directories

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

Os.path.dirname (path): Back to File path

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.