Common built-in modules for one of the common Python modules

Source: Internet
Author: User
Tags create directory exit in throw exception

1. Os

(1) os.path--commonly used to extract and separate file names

Os.path.basename (' path ') extracts the file base name

Os.path.dirname (' path ') extracts the path name, removing the base name

Os.path.split (' path ') separates the base name from the path name

Os.path.splitext (' path '), except that the base name in the returned tuple is before the path name

Os.path.join (' A ', ' B ', ' C ') will be merged into ' a/b/c ' file name

Os.exists (' path ') querying for file existence

Os.path.isfile (file) determines whether the specified object is a file

Os.path.isdir (dir) determines whether the specified object is a directory

See Os.path with many methods. [Tab] [Tab]

(2) Os.system (CMD)--Execute shell Command example: Os.system (' pwd ') (3) OS.GETCWP ()--Get current directory (4) os.mkdir (dir) Create directory (5) os.remove (file) Delete specified File (6) os.rmdir (dir) Delete the specified directory (7) os.stat (file) Gets the files information 2, SYS (1) sys.exit (n): When executed to the end of the main program, the interpreter exits automatically. But if you need to exit the program halfway, you can call the Sys.exit function, which is returned with an optional integer argument to the program that called it. This means that you can capture calls to Sys.exit in the main program.     (Note: 0 is normal exit, the other is not normal, can throw exception event for capture!) (2) sys.argv[] give the program parameters, the first parameter is the program path itself example: test.py script content: Import sys print SYS.ARGV[0] Print sys.argv[1] Print sys.argv[2] print sys.argv[3] [[email protected] scripts]# python test.py arg1 arg2 A RG3 test.py arg1 arg2 arg3 (3) sys. Path View module Search path Add module search path: 1) Dynamic temporarily add import sys sys.path.append (Tmppath) 2) in Python. pth file increased under Site-packages path Add. pth files, each row of records in the file represents a valid path 3) Pythonpath adds a module path pythonpath is a system environment variable that can be identified by all Python, such as installing python2.7 and python3.4 at the same time (4) Sys.platform returns the current system platform, such as: Win32, Linux, and so on. 3, Urllib, URLLIB2   Urllib2 can accept a request object and use this to set the headers of a URL, but urllib only receives one URL.  This means that you cannot disguise your user-agent string and so on. The Urllib module can provide a method for UrlEncode, which is used to generate a get query string, and URLLIB2 does not have such a function.  This is why Urllib and urllib2 are often used together. (1) Urllib2.urlopen (Url[,data,timeout]) is used to obtain Web content (2) urllib2. Response (URL) provides a Response that can be used in Urllib2.urlopen (3) Urllib2.urlopen (Response) to obtain the request, (2), (3) step-up and (1) effect (4) Urllib.urlopen (URL) effect with (1) the Read () method is available after fetching the content of the Web page 4, re see Python Regular expression Here is just a list of commonly used modules of common methods, specific use of methods and cases, please Google

Common built-in modules for one of the common Python modules

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.