Learn about common modules that Python must know

Source: Internet
Author: User

A program or subroutine required to complete a function in a program, or a stand-alone program unit that can be processed by a compiler, assembly, etc., or part of a large software system. This article introduces you to two common modules in Python.

Os

This module contains common operating system functions. This module can be used to write platform-independent programs, such as using OS.SEP to replace operating system-specific path separators.

List some of the methods commonly used in OS modules:

Os.name: Gets the current system platform, Windows returns ' NT ', and Linux returns ' POSIX '.

OS.LINESEP: Gets the line terminator used by the current platform. Windows returns '/r/n ', Linux uses '/n '.

OS.GETCWD (): Gets the current working directory, which is the directory path of the current Python script work.

Os.listdir (PATH): Returns all file and directory names under the specified directory.

For example:

Python code

1 >>> os.listdir(‘/home/shirley/‘)

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

The Os.system () function is used to run the shell command. This command makes it easy to invoke or execute other scripts and commands

For example:

Python code

1234 #打开记事本  >>>os.system(‘notepad‘#打开指定的文件  >>>os.system(‘notepad shirley_python.txt‘)

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

For example:

Python code

12 >>> os.path.split(‘/home/shirley/myself/code/icbc.txt‘(‘/home/shirley/myself/code‘, ‘icbc.txt‘)

The Os.path.isfile () and Os.path.isdir () functions respectively verify that 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.

Sys

The SYS module has many functions and can be referred to in Python document Http://docs.python.org/library/sys.html.

To enumerate the usage of some commonly used functions:

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

For example:

The contents of the print.py script are:

Python code

1234 importsys print sys.argv[0print sys.argv[1printsys.argv[2]

Execute in the Interpreter:

Python code

1 >>>python print.py arg1 arg2

In general, Argv[0] represents the file name of the executed program, i.e. print.py,argv[1],argv[2] corresponding to the ARG1,ARG2 in the Interpreter command respectively.

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.

Learn about common modules that Python must know

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.