Common modules that must be known when learning Python.

Source: Internet
Author: User

Common modules that must be known when learning Python.

In programming, a program or subroutine is required to complete a function, or an independent program unit that can be processed by a Compilation Program, assembly program, or a part of a large software system. This article introduces two common modules in Python.

OS:

This module contains common operating system functions. This module can be used to write platform-independent programs. For example, OS. sep can be used to replace the specific path delimiter of the operating system.

List common methods in the OS module:

OS. name: Obtain the current system platform. 'nt 'is returned in Windows, and 'posix' is returned in Linux '.

OS. linesep: Get the row Terminator used by the current platform. In Windows, '/r/N' is returned. In Linux,'/N' is used '.

OS. getcwd (): Get the current working directory, that is, the directory path of the current python script.

OS. listdir (path): returns the names of all files and directories in the specified directory.

For example:

Python code

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

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

The OS. system () function is used to run shell commands. This command can be used to conveniently call or execute other scripts and commands.

For example:

Python code

1234 # Open notepad>>>os.system('notepad'# Open a specified file>>>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 verify whether the given path is a file or a directory.

Similarly, the OS. path. existe () function is used to check whether the GIVEN path actually exists.

Sys:

The sys module has many functions. For more information, see the python documentation http://docs.python.org/library/sys.html.

The following describes how to use common functions:

Sys. argv: transmits parameters from outside the program.

For example:

The content of the print. py script is:

Python code

1234 import sys print sys.argv[0print sys.argv[1print sys.argv[2]

Run the following command in the interpreter:

Python code

1 >>>python print.py arg1 arg2

In general, argv [0] indicates the name of the program to be executed, that is, print. py, argv [1], and argv [2] correspond to arg1 and arg2 In the interpreter commands respectively.

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.

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.