Python: Introduction to judging the current operating system type and OS/sys/platform module, pythonplatform

Source: Internet
Author: User
Tags exit in

Python: Introduction to judging the current operating system type and OS/sys/platform module, pythonplatform
Determine the operating system type

# Coding = utf-8import platformdef TestPlatform (): print ("---------- Operation System --------------------------") # Get the Python version print platform. python_version () # obtain the executable program structure of the operating system, ('32bit ', 'windowsp') print platform. architecture () # Name of the computer's network, 'acer-pc' print platform. node () # obtain the operating system name and version number, 'windows-7-6.1.7601-SP1 'print platform. platform () # computer processor information, 'intel64 Family 6 Model 42 Stepping 7, GenuineIntel 'print platform. processor () # obtain the Python build date print platform in the operating system. python_build () # obtain the information of the python interpreter in the system print platform. python_compiler () if platform. python_branch () = "": print platform. python_implementation () print platform. python_revision () print platform. release () print platform. system () # print platform. system_alias () # obtain the operating system version print platform. version () # contains all the preceding information. print platform. uname () def UsePlatform (): sysstr = platform. system () if (sysstr = "Windows"): print ("Call Windows tasks") elif (sysstr = "Linux"): print ("Call Linux tasks ") else: print ("Other System tasks") if _ name _ = "_ main _": TestPlatform () UsePlatform ()
Python OS modules related to operating systems

The OS module in the Python standard library mainly involves common operating system functions. It can be run in Linux or Windows and has nothing to do with the platform.
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

Sys module

Sys. argv: transmits parameters from outside the program.
Sys. exit ([arg]): exit in the middle of the program. If arg = 0, exit normally.
Sys. getdefaultencoding (): obtains the current encoding of the system. The default value is ascii.
Sys. setdefaultencoding (): sets the default system encoding. This method is not displayed when dir (sys) is executed. If the method fails to be executed in the interpreter, you can run reload (sys) first ), when setdefaultencoding ('utf8') is executed, the default system encoding is set to utf8. (See setting the default system encoding)
Sys. getfilesystemencoding (): obtains the encoding method used by the file system. 'mbcs 'is returned in Windows, and 'utf-8' is returned in mac '.
Sys. path: gets the string set of the specified module search path. You can put the written module under a specific path, and find it correctly during import in the program.
Sys. platform: Obtain the current system platform.
Sys. stdin, sys. stdout, sys. stderr stdin, stdout, and stderr variables contain the stream object corresponding to the standard I/O Stream. if you need to better control the output, and print cannot meet your requirements, they are what you need. you can also replace them. In this case, you can redirect the output and input to another device, or process them in a non-standard way.

Paltform Module

Platform. system () obtains the operating system type, such as windows and linux.
Platform. platform () Get operating system, Darwin-9.8.0-i386-32bit
Platform. version () Obtain System version Information 6.2.0
Platform. mac_ver ()
Platform. win32_ver () ('post2008server', '6. 000000', ", u'multiprocessor free ')

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.