Python study notes twelve_common modules

1. os

import os #Some operations on the operating system 
print (os.getcwd()) #Get the current working directory 
os.chmod( ' /usr/local ' ,7) #Add permissions to linux files/directories, which is not easy to use under windows ,1 execute 2 write 4 read 
os.chdir( ' ../ ' ) #Change the current directory (back to the previous directory), no return value 
print (os.curdir) #Current directory , .print ( 
os.pardir ) #Parent directory, .. 
os.makedirs( ' aa/bb ' ) #Create a folder recursively, create it when the parent directory does not exist, cannot create a folder when the folder already exists 
os.mkdir( ' cc ' ) #Create a folder, Cannot create folder when it already exists
os.removedirs( ' aa/bb ' ) #recursively delete empty directories 
os.rmdir( ' cc ' ) # delete the specified empty folders 
os.remove( ' temp.py ' ) # delete files 
print (os.listdir() ) #List all files and folders in the current directory 
print (os.listdir( ' d:/ ' )) #List all files in the d drive os.rename 
( ' test ' , ' test1 ' ) #Rename print (os.stat( ' tmp.py ' ))
#Get file information 
print (os.sep) #The path separator of the current operating system, win:\,linux:/ 
# day5+os.sep+tmp.py Use os.sep to splice paths to improve system compatibility 
print (os .linesep) #The newline character of the current operating system,\n \r\n 
print (os.pathsep) #The separator of each path in the environment variable of the current system, linux is:, windows is; 
print (os.environ) #Environmental variables of the current system print (os.name) #The current system name, Windows systems are both nt and linux are all posix, used to determine what system is currently print (os.system( ' dir ' )) #Execute operating system commands Yes , check what is in the current directory dir, used to perform backup under linux 
res = os.system( ' dir ' ) #

The command execution result cannot be obtained. Returning 0 means the command execution is successful. Commands used to create folders and other commands that do not need to return results. 
res = os.popen( ' ifconfig ' ).read() #You can get the result of command execution 
os. open( ' top ' ).read() #Cannot be executed because the execution result of the top command has been changing dynamically 
os.popen( ' top -n 1 ' ).read() #Only take it once, you can get the result 
print (os. path.abspath( ' . ' )) #Get the absolute path 
print ( __file__ ) #Get the absolute path to the current file, but the path separator is wrong 
print (os.path.abspath( __file__ )) #Get the absolute path to the current file 
print(os.path.split( ' /usr/hehe/hehe.txt ' )) #Split the path and file name, take list[1] to get the file name directly 
print (os.path.dirname( ' d:\\ work ' )) #Get the parent directory, get its parent directory, d:\ 
print (os.path.basename( ' d:\\work ' )) #Get the last level, if it is a file, display the file name, If it is a directory, display the directory name, work 
print (os.path.exists( ' d:\\work ' )) #Whether the directory/file exists, True 
print (os.path.isabs( ' d:\\work ' )) #Determine whether it is an absolute path, True 
print (os.path.isfile( 'tmp.py ' )) #Determine whether it is a file, 1. The file must exist 2. It must be a file 
print (os.path.isdir( ' d:\\work ' )) #Whether it is a path, whether the directory exists ,True 
print (os.path.getsize( ' tmp.py ' )) #Get the size of the file, return bytes, which can be used to limit the file upload size 
print (os.path.join( ' root ' , ' db ' , ' a.sql ' )) #Spliced ​​into a path, the delimiter will be automatically recognized, and the consistency with os.sep is more intelligent, root\db\a.sql 
print (os.path.getatime( ' tmp.py ' )) #Output the last access time 
print (os.path.getmtime( ' tmp.py ' )) #Output the last access time

 os.walk is a very powerful method that can traverse the files in subfolders under a folder

os.walk returns a two-dimensional array. Each element of the array contains three elements: absolute path, folder, and file. Three variables can be used to loop 

for data in os.walk(r ' d:\workspace\python\syz-dongrui\Day6 ' ):
     print (data)
 for abs_path,dir,file in os.walk(r ' d:\workspace\python\syz- dongrui\Day6 ' ): #Get the contents of the directory #Add an r in front of the path 
    , and will not escape /n etc. # os.listdir() lists all files in the current directory, os.walk will traverse subfolders to find All files print (abs_path,dir,file)
     # abs_path The absolute path of the current loop # All folders under the dir directory [ ] #   All files under the file directory []
    
    
    
    

Two, sys

Three, random

import random
 print (random.randint(1,10)) #Random integers 
print (random.randrange(1,20)) #Randomly generate a range 
print (random.random()) #Random floating point numbers, the default is 0- 1. The range cannot be specified 
print (random.uniform(1,3)) #Random decimals , you can specify the range 
print (round(random.uniform(1,99),2)) #Random decimals , keep two decimal places 
print ( random.choice([1,2,3])) #Randomly pick 1 element 
print (random.sample( ' hello ' ,3)) #Randomly pick N elements and return list 
pickts = [ ' A ' , ' J','Q','K',2,3,4,5,6]
random.shuffle(pickts) #Shuffle can only pass list print (pickts) # ['Q' , 'A', 2, 6, 3, 4, 5, 'K', 'J']

Four, string

import string
 print (string.ascii_letters) #all uppercase and lowercase letters 
print ( string.ascii_lowercase ) #all lowercase letters 
print (string.ascii_uppercase) #all uppercase letters 
print (string.digits) #all numbers print 
( string.punctuation ) #all Special punctuation 
print (string.printable) #Number + letter + special character

Five, time

Six, hashlib

Seven, json

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325034211&siteId=291194637