The path of Python (v)

Source: Internet
Author: User
Tags local time python script timedelta


Regular expressions

Use the Compile method to match the regular object

Equivalent to M=re.match ("^[0-9", "18ruue");p rint M.group ()


Re.match
Re.search
Re.sub
Re.findall
Re.split

Re.search----> Group



Re.split


Re.findall


Re.sub




Bubble sort




Module
The file in the module for others to call is best not to perform initialization objects, such as the TV () in the example below
index.py execution will error, saying that there is no module





This file will be called by main.py, and for main.py it will recognize Backend.db.sqlApi.select, Because its current path Sys.path next level is backend. So it calls index.py without error, and also executes the statement TV () in index.py.


The correct wording is to remove the TV () in index.py



What should I do if I call from my superiors or superiors?
Goupper.py uses from conf import settings, You can see that the Conf directory is goupper.py, and Conf is under the Selfdefinemodule directory, so you need to add the Selfdefinemodule directory to Sys.path.
__FILE__ is the print relative path, Os.path.abspath () prints the absolute path, Os.path.dirname () is the previous level of the print directory

Os.path.dirname ()
Os.path.abspath (__file__)



Time and DateTime module

Import time
Import datetime

Print (Time.clock ()) #返回处理器时间, 3.3 started obsolete
Print (Time.process_time ()) #返回处理器时间, 3.3 started obsolete
Print (Time.time ()) #返回当前系统时间戳
Print (Time.ctime ()) #输出Tue Jan 26 18:23:48 2016, current system time
Print (Time.ctime (Time.time () -86640)) #将时间戳转为字符串格式
Print (Time.gmtime (Time.time () -86640)) #将时间戳转换成struct_time格式
Print (Time.localtime (Time.time () -86640)) #将时间戳转换成struct_time格式, but the local time returned
Print (Time.mktime (Time.localtime ())) #与time. LocalTime () function instead, turn struct_time format back into timestamp format
#time. Sleep (4) #sleep
Print (Time.strftime ("%y-%m-%d%h:%m:%s", Time.gmtime ())) #将struct_time格式转成指定的字符串格式
Print (Time.strptime ("2016-01-28", "%y-%m-%d")) #将字符串格式转换成struct_time格式

#datetime Module

Print (Datetime.date.today ()) #输出格式 2016-01-26
Print (Datetime.date.fromtimestamp (Time.time () -864400)) #2016-01-16 turn the timestamp into a date format
Current_time = Datetime.datetime.now () #
Print (current_time) #输出2016-01-26 19:04:30.335935
Print (Current_time.timetuple ()) #返回struct_time格式

#datetime. replace ([year[, month[, day[, hour[, minute[, second[, microsecond[, Tzinfo] []] []])
Print (Current_time.replace (2014,9,12)) #输出2014-09-12 19:06:24.074900, returns the current time, but the specified value is replaced

Str_to_date = Datetime.datetime.strptime ("21/11/06 16:30", "%d/%m/%y%h:%m") #将字符串转换成日期格式
New_date = Datetime.datetime.now () + Datetime.timedelta (days=10) #比现在加10天
New_date = Datetime.datetime.now () + Datetime.timedelta (days=-10) #比现在减10天
New_date = Datetime.datetime.now () + Datetime.timedelta (hours=-10) #比现在减10小时
New_date = Datetime.datetime.now () + Datetime.timedelta (seconds=120) #比现在 +120s
Print (new_date)







Random module

Random.random ()
Random.randint (1,5)
Randon.randrange (1,5)




Import Random
Import re
Bb= "

For I in range (4):
AA = Random.randint (0,4)
If AA! = I:
TMP = Chr (Random.randrange (65,90))
Else
TMP = Random.randint (0,9)
BB+=STR (TMP)
Print (BB)
A = Re.findall ("[0-9]", BB)
b = Re.findall ("[A-z]", BB)
If a:
If Len (a) > 2:
Bb=re.sub ("[0-9]", str (CHR (Random.randrange (65,90))), bb,count=1)
If B:
If Len (b) > 2:
Bb=re.sub ("[A-z]", str (Random.randrange (0,9)), bb,count=1)


Print (BB)






OS Module
Provides an interface to invoke the operating system


OS.GETCWD () Gets the current working directory, which is the directory path of the current Python script work
Os.chdir ("dirname") changes the current script working directory, equivalent to the shell CD
Os.curdir returns the current directory: ('. ')
Os.pardir Gets the parent directory string name of the current directory: (' ... ')
Os.makedirs (' dirname1/dirname2 ') can generate multi-level recursive directories
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.mkdir (' dirname ') generates a single-level directory, equivalent to mkdir dirname in the shell
Os.rmdir (' dirname ') delete the single-level empty directory, if the directory is not empty can not be deleted, error, equivalent to the shell rmdir dirname
Os.listdir (' dirname ') lists all files and subdirectories in the specified directory, including hidden files, and prints as a list
Os.remove () Delete a file
Os.rename ("Oldname", "newname") renaming files/directories
Os.stat (' path/filename ') get File/directory information
OS.SEP output operating system-specific path delimiter, win under "\ \", Linux for "/"
OS.LINESEP output The line terminator used by the current platform, win under "\t\n", Linux "\ n"
OS.PATHSEP output string for splitting the file path
The Os.name output string indicates the current usage platform. Win-> ' NT '; Linux-> ' POSIX '
Os.system ("Bash command") runs a shell command that directly displays
Os.environ Getting system environment variables
Os.path.abspath (path) returns the absolute path normalized by path
Os.path.split (path) splits path into directory and file name two tuples returned
Os.path.dirname (path) returns the directory of path. is actually the first element of Os.path.split (path)
Os.path.basename (Path) 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 path exists, false if path does not exist
Os.path.isabs (path) returns True if path is an absolute path
Os.path.isfile (path) returns True if path is a file that exists. otherwise returns false
Os.path.isdir (path) returns True if path is a directory that exists. otherwise returns false
Os.path.join (path1[, path2[, ...]) returns a combination of multiple paths, and the parameters before the first absolute path are ignored
Os.path.getatime (Path) returns the last access time of the file or directory to which path is pointing
Os.path.getmtime (Path) returns the last modified time of the file or directory to which path is pointing





SYS module

SYS.ARGV command line argument list, the first element is the path of the program itself
Sys.exit (n) exit program, Exit normally (0)
Sys.version get version information for 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.stdout.write (' please: ')
val = Sys.stdin.readline () [:-1]











Sys.stdout.flush () is to let the # print one after the other, not all output to the buffer after the last print together




Pickle and JSON
Pickle only exists in Python; Pickle supports many data types, such as dict, List, function, module
JSON supports all language; JSON supports basic data type





Http://www.cnblogs.com/alex3714/articles/5161349.html


The path of Python (v)

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.