Python built-in modules

Source: Internet
Author: User

Python built-in modules

# Continuous update

# Import a built-in module, for example, import abc, to import the abc module. Of course, the module can also be written by itself, which is equivalent to a class, this is because the environment is idle and some cannot be executed.

# Operating System

Import OS

OS. system ('LS') # Call the system command and return the execution result. OS. system ('dir ')....

OS. chdir ('/home/myuser/py') # in windows, you can directly compress the path into 'C: \ mypy'. The OS can change the path to a common path.

Dir_path = OS. getcwd () # obtain the current directory. The result is the current directory path '/home/myuser/py'

OS. listdir (dir_path) # obtain all files and folders in the specified directory. The result is a list

OS. path. isdir (dir_name) # determines whether the specified name is a folder. If dir_name is a folder, True is returned; otherwise, False is returned.

OS. path. join (dir1, dir2, file1) # Merge multiple paths, which can be dir1, dir2...., file1

OS. mkdir ('py') # create a directory. Like linux, there is nothing to say.

OS. rmdir ('py') # The Directory to be deleted must be an empty directory, which is the same as that in linux.

 

# Re regular operation

Import re

A = "my py it's fucking greate! "

# Several common regular expressions, | or,. wildcard (same as excel *),? Matches 0 or 1, * matches 0 or more, + matches 1 or more, \ symbol \, \ * For example, if you want to match *, you need to use \ escape to be only one * instead of zero or more. ^ matches the beginning of a row and $ matches the end of a row.

#(? <= XXX) forward view ,(? = XXX) postview: this can be Baidu. I can't tell you clearly, but it will not be used. It is usually used for crawlers.

# [A-Z] all uppercase letters, [a-z] All lowercase letters, [0-9] All numbers

# The default regular expression is greedy mode ,.*? This write mode is not greedy, and (XXX) matches a string.

Re_value = re. compile ('^ .*? ') # Compile the regular expression. This regular expression indicates matching the content from the start to the first space. It is best to compile the regular expression first and then use it.

Re_search = re. search (re_value, a) # Find the regular expression matching in the string. This cannot be directly displayed and requires group

Print re_search.group () # The result is my

Re_find = re. findall (re_value, a) # Find all matching results in the string and return an iteration

For I in re_find:
Print I # because there is only one row, only one row is found. The result is my. You can try multiple rows by yourself.

Re. sub (re_value, '', a) # Replace re_value with'', which is to replace the result of regular expression matching with null. Of course, it can also be replaced with something else, the result is "py it's fucking greate! "

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.