Python Study --- Focus on template learning
import time
# print(help(time)) # time Module help
print(time.time()) # Time stamp
print(time.clock()) # Calculation CPU Execution time
print(time.ctime()) # Default current time Sun Dec 10 22:07:16 2017
print(time.ctime(1512914742)) # Seconds to time Sun Dec 10 22:05:42 2017
print(time.mktime(time.localtime())) # Convert current time to timestamp
print(time.strftime('%F %X')) # 2017-12-10 22:05:42
print(time.localtime()) #time.struct_time(tm_year=2017, tm_mon=12, tm_mday=10, tm_hour=22, tm_min=4, tm_sec=47, tm_wday=6, tm_yday=344, tm_isdst=0)
time.sleep(): Do not call CPU resources
import datetime
import time
print(datetime.datetime.now()) # 2017-12-10 22:10:51.364760
print(time.ctime()) # Sun Dec 10 22:12:23 2017
print(time.strftime('%F %X')) # 2017-12-10 22:05:42
Overtime :
import datetime
timeout = datetime.datetime.now() + datetime.timedelta(seconds=10) # 10 second
timeout2 = datetime.datetime.now() + datetime.timedelta(days=10) # 10 God ·
import random
print(random.random()) # Value range [0-1]
print(random.random()*100) # Value range [0-100]
print(random.randint(1, 8)) # Value range [1-8]
print(random.choice('hello')) # Value range [hello]
print(random.choice(['hello', 'world', '2017'])) # Value range ['hello', 'world', '2017']
print(random.sample(['1', '2', '3', '5'], 2)) # Value range from ['1', '2', '3', '5'] Remove from 2 It's worth
print(random.randrange(1, 7)) # Value range [1, 7), Left closed right away , recommend
import os
os.getcwd() Get the current working directory , At present python Directory path for script work
os.chdir("dirname") Change the current script working directory ; amount to shell Next cd
os.curdir Return to the current directory : ('.')
os.pardir Get the parent directory string name of the current directory :('..')
os.makedirs('dirname1/dirname2') Can generate multi-level recursive directory
os.removedirs('dirname1') If the directory is empty , Delete , And recurse to the next level directory , If it's empty , Delete , And so on
os.mkdir('dirname') Generate a single level directory ; amount to shell in mkdir dirname
os.rmdir('dirname') Delete single level empty directory , If the directory is not empty, you cannot delete , Report errors ; amount to shell in rmdir dirname
os.listdir('dirname') List all files and subdirectories in the specified directory , Including hidden files , And print... In a list
os.remove() Delete a file
os.rename("oldname","newname") Rename file / Catalog
os.stat('path/filename') get files / catalog information
os.sep Output OS specific path separators ,win for "\\",Linux for "/"
os.linesep Output the line terminator used by the current platform ,win for "\t\n",Linux for "\n"
os.pathsep Output the string used to split the file path
os.name The output string indicates the current platform of use .win->'nt'; Linux->'posix'
os.system("bash command") function shell command , Direct display
os.environ Get system environment variables
os.path.abspath(path) return path The absolute path of normalization
os.path.split(path) take path Split into directory and file name and return
os.path.dirname(path) return path The catalog of . In fact, that is os.path.split(path) The first element of
os.path.basename(path) return path Last filename . how path With / or \ ending , Then it will return a null value . namely os.path.split(path) The second element of
os.path.exists(path) If path There is , return True; If path non-existent , return False
os.path.isabs(path) If path It's the absolute path , return True
os.path.isfile(path) If path It's an existing file , return True. Otherwise return to False
os.path.isdir(path) If path Is an existing directory , Then return to True. Otherwise return to False
os.path.join(path1[, path2[, ...]]) Combine multiple paths and return , Parameters before the first absolute path will be ignored
os.path.getatime(path) return path The last access time of the pointed file or directory
os.path.getmtime(path) return path The last modification time of the file or directory pointed toprint('parent process:', os.getppid())
print('process id:', os.getpid())
import os
import time
os.chdir(r'c:\Users') # r For regular ,Py The built-in function will automatically replace this , Means input is output , Do not replace special characters
# os.removedirs('test\\tt') # Only empty folders can be deleted , If there is content in the folder , Then return to the previous level to find
# os.makedirs('test\\tt') # With a \ Represents an escape character , It means that c:\User Created test,test I created tt
# os.mkdir('hhh') # Create a single folder
# os.rmdir('hhh') # Delete a single empty folder
# os.rmdir('test\\tt') # Delete a single empty folder tt, Delete only tt A folder
# os.remove('test\hhh.txt') # Delete test Below hhh.txt file
# os.renames('test\\tt', 'test\\ttt') # Rename file / Folder
# print(os.sep) # File separator ,Win:\, Linux: /
# print('os.linesep: ', os.linesep) # Newline separator , Win:\r\n Linux:\n
# print(os.pathsep) # Path separator , For example, environment variables , Win:; Linux: :
# print('os.name: ', os.name) # The character indicates the currently used system name , Win:nt Linux:posix
# print(os.system('dir')) # Execute system commands
# print(os.environ) # Display system environment variables
# print(os.stat('test\\ttt\hhh.txt')) # Get information about the file , Including the creation time of the file , Change and other information
# print(os.stat('test'+os.sep+'ttt'+os.sep+'hhh.txt'))
# print(os.listdir(os.getcwd()))# Get the content under the current path
# print(os.path.abspath('./test')) # Show the absolute path of the file
# print(os.path.split(r'c:\Users\test\ttt\hhh.txt')) # Cut the file path and file , Returns a tuple
# print(os.path.dirname(r'c:\Users\test\ttt\hhh.txt')) # Return the path name where the file is located
# print(os.path.basename(r'c:\Users\test\ttt\hhh.txt')) # Return the file name where the file is located
# print(os.curdir) # Get the current path ,. Represents the current path ,.. On behalf of the previous Directory
# print(os.getcwd()) # amount to Linux Under the pwd
# os.mkdir('hhh') # Create folder in current path
print('parent process:', os.getppid()) # Get parent process number
print('process id:', os.getpid()) # Get your own process number
sys.argv Command line arguments List, The first element is the path of the program itself
sys.exit(n) Exit procedure , On normal exit exit(0)
sys.version obtain Python Explain the version information of the program
sys.maxint maximal Int value
sys.path Return the search path of the module , Use... When initializing PYTHONPATH The value of the environment variable
sys.platform Return operating system platform name
sys.stdout.write('please:')
val = sys.stdin.readline()[:-1]
import sys
# print(sys.version) # obtain Python Explain the version information of the program
# print(sys.maxsize) # maximal Int value 9223372036854775807
# print(sys.platform) # Return operating system platform name win32
# print(sys.path) # Return the search path of the module , Use... When initializing PYTHONPATH The value of the environment variable
# print(sys.path.append(' Absolute path '))
# print(sys.stdout.write(sys.version)) # standard output
# val = sys.stdin.readline()[:-1]# Standard write
# print(val)
MD5 encryption algorithm : A one-way , Irreversible algorithm
import hashlib
m = hashlib.md5() # establish hash object
# Py3 Default is unicode code , What we need is a character bytes type [ Binary system ], So you need to switch
m.update('hello world'.encode('utf-8'))
print(m.digest()) # Hexadecimal binary display
print(m.hexdigest()) # Returns a hexadecimal [0-9a-f] In plain text 5eb63bbbe01
Sha256 encryption algorithm : A one-way , Irreversible algorithm
import hashlib
# sha256 encryption algorithm
sha = hashlib.sha256() # sha256 Most used
sha.update('hello world'.encode('utf-8'))
print(sha.hexdigest()) # b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
Python Study --- Key modules logging
Python Study --- Key modules configparse
Python Study --- Key modules re
Python Study --- Key modules subprocess
Generate a string according to the model and other characteristics , Generally speaking, it is not repeated
import uuid
print(uuid.uuid1()) # Unique representation
# print(uuid.uuid4()) # Will be generated randomly
Python Study --- Key modules json
Python Study --- Key modules pickle
Python Study --- Key modules shelve
Python Study --- Key modules xml
author : Small a ninety-seven
-------------------------------------------
Individuality signature : Everything is good in the end , If it's not good , That means things haven't come to the end yet ~
The copyright of this article belongs to the author 【 Small a ninety-seven 】, Welcome to reprint , However, this statement must be retained without the consent of the author , And in the article page obvious position gives the original link , Otherwise, the right to pursue legal responsibility is reserved !