First, OS
ImportOs#some operations on the operating systemPrint(OS.GETCWD ())#fetch Current Working directoryOs.chmod ('/usr/local', 7)#add permissions to Linux files/directories, bad for Windows, 1 perform 2 write 4 readOs.chdir ('.. /')#Change current directory (back to top level), no return valuePrint(Os.curdir)#current directory,.Print(Os.pardir)#parent directory,. .Os.makedirs ('aa/bb')#You cannot create a folder when it is created recursively, when the parent directory does not exist, when the folder already exists.Os.mkdir ('cc')#cannot create a folder when a folder already existsOs.removedirs ('aa/bb')#recursively delete empty directoriesOs.rmdir ('cc')#Delete the specified empty folderOs.remove ('temp.py')#Deleting FilesPrint(Os.listdir ())#list all files and folders in the current directoryPrint(Os.listdir ('d:/'))#list all files on D driveOs.rename ('Test','test1')# RenamingPrint(Os.stat ('tmp.py'))#Get file InformationPrint(OS.SEP)#path delimiter for the current operating system, win:\,linux:/#day5+os.sep+tmp.py with OS.SEP stitching path for improved system compatibilityPrint(OS.LINESEP)#line break for the current operating system \ n \ nPrint(OS.PATHSEP)#The delimiter for each path in the current system's environment variables, Linux is:, Windows is;Print(Os.environ)#environment variables for the current systemPrint(Os.name)#the current system name, Windows system is all NT Linux is POSIX, used to determine what the current systemPrint(Os.system ('dir'))#perform operating system commands, see what's in the current directory dir, used to perform Linux under Backupres = Os.system ('dir')#Unable to get command execution result, return 0 for command execution success, used to create folder, etc. do not need to return the result of the commandres = Os.popen ('ifconfig'). Read ()#you can get the results of the command executionOs.open ('Top'). Read ()#cannot be executed because the top command execution result has been dynamically changingOs.popen ('Top-n 1'). Read ()#take only one time to get resultsPrint(Os.path.abspath ('.'))#Get absolute pathPrint(__file__)#gets the absolute path to the current file, but the path delimiter is notPrint(Os.path.abspath (__file__))#gets the absolute path to the current filePrint(Os.path.split ('/usr/hehe/hehe.txt'))#split path and file name, take list[1] directly can be removed file namePrint(Os.path.dirname ('d:\\work'))#gets the parent directory, gets its top level directory, D:\Print(Os.path.basename ('d:\\work'))#gets the last level, if the file displays the filename, and if the directory displays the directory name, workPrint(Os.path.exists ('d:\\work'))#whether the directory/file exists, TruePrint(Os.path.isabs ('d:\\work'))#determines if the absolute path is TruePrint(Os.path.isfile ('tmp.py'))#Determine whether it is a file, 1, file to exist 2, must be a filePrint(Os.path.isdir ('d:\\work'))#whether it is a path, whether the directory exists, TruePrint(Os.path.getsize ('tmp.py'))#gets the size of the file, returns the bytes that can be used to limit the file upload sizePrint(Os.path.join ('Root','DB','A.sql'))#stitching into a path, the delimiter is automatically recognized, and the OS.SEP is more consistent and smarter, Root\db\a.sqlPrint(Os.path.getatime ('tmp.py'))#output last access timePrint(Os.path.getmtime ('tmp.py'))#output last access time
Os.walk is a very powerful way to traverse files in subfolders under a folder
Os.walk returns a two-dimensional array in which each element contains an absolute path, a folder, three elements of a file, and three variables can be used to loop
forDatainchOs.walk (R'D:\workspace\python\syz-dongrui\Day6'): Print(data) forAbs_path,dir,fileinchOs.walk (R'D:\workspace\python\syz-dongrui\Day6'):#get content under directory #the path is preceded by an R, and the/n is not escaped #Os.listdir () lists all the files in the current directory, and Os.walk iterates through the subfolders to find all the files Print(Abs_path,dir,file)#Abs_path The absolute path of the current loop #dir directory all folders below [] #all files under the file directory []
Second, systhird, Random
ImportRandomPrint(Random.randint (1,10))#randomly take integersPrint(Random.randrange (1,20))#randomly produces a rangePrint(Random.random ())#Random floating-point number, default 0-1, cannot specify rangePrint(Random.uniform (1,3))#random Decimals, you can specify a rangePrint(Round (Random.uniform (1,99), 2))#random decimals, reserved two digits after the decimal pointPrint(Random.choice ([+]))#randomly take 1 of elementsPrint(Random.sample ('Hello', 3))#randomly takes n elements, returns a listPickts = ['A','J','Q','K', 2,3,4,5,6]random.shuffle (pickts)#Shuffle can only pass listPrint(PICKTS)#[' Q ', ' A ', 2, 6, 3, 4, 5, ' K ', ' J ']
Iv. 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) # numbers + letters + special characters
Five, timeLiu, HashlibSeven, JSON
Python Learning Note 12 _ Common Modules