Do not write easy to forget, feel a lot of strange, re-fill back
Module: A code collection that implements a function with a single piece of code
Time & DateTime Module
#_*_coding:utf-8_*_Import TimeImportdatetimePrint(Time.clock ())#returns processor Time, 3.3 started obsoletePrint(Time.process_time ())#returns processor Time, 3.3 started obsoletePrint(Time.time ())#returns the current system timestampPrint(Time.ctime ())#output Tue Jan 26 18:23:48 2016, current system timePrint(Time.ctime (Time.time ()-86640))#Convert timestamp to string formatPrint(Time.gmtime (Time.time ()-86640))#convert timestamp to struct_time formatPrint(Time.localtime (Time.time ()-86640))#converts a timestamp to the struct_time format, but returns the local timePrint(Time.mktime (Time.localtime ()))#reverse the Struct_time format to a timestamp format, as opposed to the Time.localtime () function#Time.sleep (4) #sleepPrint(Time.strftime ("%y-%m-%d%h:%m:%s", Time.gmtime ()))#converts the Struct_time format to the specified string formatPrint(Time.strptime ("2016-01-28","%y-%m-%d") )#convert string format to struct_time format #datetime Module Print(Datetime.date.today ())#output Format 2016-01-26Print(Datetime.date.fromtimestamp (Time.time ()-864400))#2016-01-16 turn timestamps into date formatsCurrent_time = Datetime.datetime.now ()#Print(Current_time)#Output 2016-01-26 19:04:30.335935Print(Current_time.timetuple ())#return to Struct_time format #Datetime.replace ([year[, month[, day[, hour[ , minute[, second[, microsecond[, Tzinfo]])Print(Current_time.replace (2014,9,12))#output 2014-09-12 19:06:24.074900, returns the current time, but the specified value will be replacedstr_to_date= Datetime.datetime.strptime ("21/11/06 16:30","%d/%m/%y%h:%m")#Convert a string into a date formatNew_date = Datetime.datetime.now () + Datetime.timedelta (days=10)#10 days longer than now .New_date = Datetime.datetime.now () + Datetime.timedelta (days=-10)#minus 10 days now .New_date = Datetime.datetime.now () + Datetime.timedelta (hours=-10)#minus 10 hours now .New_date = Datetime.datetime.now () + Datetime.timedelta (seconds=120)#+120s than now .Print(new_date)
Random module
mport Random Print random.random () print random.randint (random.randrange)print (1,10)
Generate Random Verification code
Import" for" in range (4= Random.randrange (0,4) if current == Chr (Random.randint (65,90)) else= random.randint (0,9 /c12>+= Str (temp)print Checkcode
OS Module
Provides an interface to an operating system call
OS.GETCWD () Gets the current working directory, which is the directory path of the current Python script work os.chdir ("dirname") To change the current script working directory, equivalent to the shell under Cdos.curdir return to the current directory: ('.') Os.pardir Gets the parent directory string name of the current directory: ('..') Os.makedirs ('dirname1/dirname2') to generate a multi-level recursive directory Os.removedirs ('dirname1'If the directory is empty, it is deleted and recursively to the previous level of the directory, if it is also empty, then delete, and so on Os.mkdir ('dirname') to generate a single-level directory, equivalent to the shell mkdir Dirnameos.rmdir ('dirname'Delete the single-level empty directory, if the directory is not empty can not be deleted, error, equivalent to the shell rmdir Dirnameos.listdir ('dirname'lists all files and subdirectories under the specified directory, including hidden files, and prints a list of os.remove () deletes a file Os.rename ("oldname","newname") Rename File/Catalog Os.stat ('Path/filename') Get File/directory information OS.SEP output operating system-specific path delimiter, win under"\\", under Linux for"/"os.linesep Output The line terminator used by the current platform, win under"\t\n", under Linux for"\ n"the OS.PATHSEP output string that is used to split the file path Os.name the output strings indicates the current usage platform. Win-'NT'; Linux->'POSIX'Os.system ("Bash Command"run the shell command to directly display the Os.environ get system environment variable Os.path.abspath (PATH) return path normalized absolute path Os.path.split (path) Split path into directory and file name two tuples return Os.path.dirname (path) to the directory where path is returned. 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 path exists, or if path does not exist, returns Falseos.path.isabs if path is an absolute path, Returns Trueos.path.isfile (path) If path is an existing file and returns True. Otherwise, return Falseos.path.isdir (path) True if path is a directory that exists. Otherwise return Falseos.path.join (path1[, path2[, ...]) When multiple paths are combined, 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 program itself path Sys.exit (n) exits the program, exit normally exits (0) Sys.version Gets the version information of the Python interpreter SYS.M Axint the largest int value Sys.path returns the search path for the module, initialized with the value of the PYTHONPATH environment variable Sys.platform returns the operating system platform name Sys.stdout.write ('please:'= Sys.stdin.readline () [:-1]
JSON & Pickle Modules
Two modules for serialization
- JSON, used to convert between string and Python data types
- Pickle for conversion between Python-specific types and Python data types
The JSON module provides four functions: dumps, dump, loads, load
The Pickle module provides four functions: dumps, dump, loads, load
Day6 module time datetime random OS sys JSON pikle