Python module classification

Module classification:

One, the standard library

1. Time module

a. time和datetime

Timestamp (from 1970 to the present in seconds) and formatted time representation ("2018-6-25 10:12:33")

Timestamp: print(time.time())

time.sleep(3) sleeps for three seconds

time.gmtime(), pass in a timestamp, return UTC standard time, expressed as a tuple (counted from 1970)

time.localtime(), pass in a timestamp, and return the local (UTC+8) time, expressed as a tuple (from 1970)


time.mktime(time.localtime()), converted to timestamp

time.strftime("format", struct_time) is converted to formatted time (time.gmtime and time.localtime are both struct_time)

time.strptime("formatted time string", "format") is the opposite of strftime, converting formatted time into tuple form


Format time string: print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

2. Random module

random.random() randomly returns a floating point number between 0 and 1

random.randint(1,10) randomly returns an integer between 1~10

random.randrange(1,10) randomly returns a number between 1-9

random.choice("hello") then returns a value in the sequence

random.sample("sequence",2) randomly take two values ​​from the sequence

random.uniform(1,3) randomly returns a floating point number between 1 and 3

random.shuffle(l) l is a list. Assuming that some numbers are stored in an orderly manner, after executing this sentence, the numbers in l are shuffled, which is equivalent to the shuffle function. After print(l), l is found The order of numbers in has been disrupted

3. os module

os.getcwd() Get the current operating directory

os.chdir("C:\\Users") switch the current path

os.makedirs(r"W:\a\b\c") Recursively create directories

os.removedirs(r"W:\a\b\c") If it is empty, delete the directory recursively (clean up empty folders)

os.rmdir(r"W:\a\b\c") Regardless of whether it is empty or not, only delete the c file

4. Shuil module

shutil.copyfileobj(file1,file2) copies the contents of file1 to file2

shutil.copyfile(file1,file2) is more convenient than the above statement, and the functions performed are the same

Learn more about modules and click to open the link




Two, open source modules

Three, custom modules

Guess you like

Origin blog.csdn.net/wxy_csdn_world/article/details/80797923