Python Study to 6 basics

Source: Internet
Author: User
Tags parent directory python script

Recursive

recursive factorial 1*2*3*4...7 case

def func (num):    if num = = 1:        return 1    return num * func (num-1) x =func (7) print (x)
Reflection

Reflection Definition:

To manipulate (Find/check/set or delete) a member in the form of a string, called reflection, in an object (module).

Reflection: An instance-based routing system that forges web frames. Access the URL and navigate to the corresponding function.

The method used to reflect the reflection:

    • GetAttr () Gets a member of a module
    • Hasattr () Determines whether a member is set to Ture in this module, otherwise false
    • SetAttr () sets a member in the module
    • Delattr () remove a member from the module

The action case for a function in a module in reflection:

Import Commonsdef Run ():    INP = Input ("Enter the URL to access:")    # Manipulate (Find/check/set, delete) members in the form of a string, called Reflection    delattr () Remove a member from the module; SetAttr set a member in the module. Both Delattr and SetAttr are memory-based operations    if HASATTR (COMMONS,INP):       func=getattr (COMMONS,INP)       func ()    else:        Print ("404") if __name__ = = "__main__":    Run ()

An import case in reflection that uses a string form for a module:

def run ():    INP = input ("Please enter the URL to access:")    m, f = inp.split ("/")    obj = __import__ (m)    if Hasattr (obj,f): C13/>func=getattr (obj,f)       func ()    else:        print ("404") if __name__ = = "__main__":    Run ()
Module

The built-in module is a Python-brought module that needs to be "imported" and "used" before use.

One, sys module

Used to provide actions related to the Python interpreter

SYS.ARGV        command line argument list, the first element is the program itself path Sys.exit (n)     exits the program, exit normally (0) sys.version     Gets the version information of the Python interpreter Sys.maxint      the largest int value Sys.path        returns the search path for the module, using the value of the PYTHONPATH environment variable when initializing Sys.platform    Returns the operating system platform name Sys.stdin       input related sys.stdout      output related sys.stderror    error related

Progress bar:

Import timedef View_bar (num, total): rate    = num/total    rate_num = Int (rate*100)    r = "\r%s%d%%"% ("=" *num,r Ate_num)    Sys.stdout.write (R) #不加换行符    Sys.stdout.flush () #输出清空if __name__ = = ' __main__ ': for    i  in Range (0,101):        time.sleep (0.1)        View_bar (i,100)

Second, OS module

OS modules are used to provide system-level operations

S.GETCWD () Gets the current working directory, that is, the current Python script working directory path Os.chdir ("DirName") changes the current script working directory, equivalent to the shell Cdos.curdir return the current directory: ('. ') Os.pardir Gets the parent directory string name of the current directory: (' ... ') Os.makedirs (' Dir1/dir2 ') can generate a multi-level recursive directory Os.removedirs (' dirname1 ') if the directory is empty, then delete, and recursively to the previous level of the directory, if also empty, then delete, and so on Os.listdir (' DirName ') lists all files and subdirectories under the specified directory, including hidden files, and prints os.remove () Delete a file Os.rename ("Oldname", "newname") Rename File/directory Os.stat (' path/ FileName ') Gets the file/directory information os.sep the operating system-specific path delimiter, win under "\ \", Linux under "/" os.linesep the current platform using the line terminator, win under "\t\n", Linux "\ n" OS.PATHSEP the string OS used to split the file path. The Name string indicates the current usage platform. Win under "NT", Linux under "Posit" Os.system ("Bash command") run shell command, direct display Os.environ get system environment variablesOs.path.abspath (path) returns path normalized absolute path Os.path.split (path) splits path into directory and file name two yuan ancestor returnsOs.path.dirname (path) returns the directory of path. In fact, the first element of Os.path.split (path) os.path.basename returns the last file name of path. If path ends with a/or \, then a null value is returned. The second element of Os.path.split (path) os.path.exists (path) returns true if it exists, or if it does not, returns Falseos.path.isabs (path) If path is an absolute path, Returns Trueos.path.isfile (path) If path exists, returns True, otherwise Falseos.path.isdir (path) returns True if path is a directory that exists. otherwise returns falseOs.path.join (path1[,path2[,...]) A combination of multiple paths is returned, and the parameters before the first absolute path are ignoredOs.path.getatime (Path) returns the last access time of the file or directory to which path is pointing
Note: The method used to remember the part of the red font

  

Python Study to 6 basics

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.