The path to python: wupeiqi

Source: Internet
Author: User
Tags timedelta

The path to python: wupeiqi

1. Time Module

1 import time 2 import datetime 3 4 # Real CPU running time 5 # print (time. clock () 6 7 # Return the time difference from utc time, calculated in seconds 8 # print (time. altzone) 9 10 # return time format "Mon Jan 23 14:55:20 2017" 11 # print (time. asctime () 12 13 # returns the struct time object format of local time 14 # print (time. localtime () 15 16 # timestamp 17 # print (time. time () 18 19 # returns the struc time object format of utc time 20 # print (time. gmtime (time. time ()-800000) 21 22 # return time format "Fri Aug 19 11:14:16 2016" 23 # print (time. asctime (time. localtime (time. time () + 3600) 24 25 # returns the current time 26 # print (time. ctime () 27 28 # convert a date string to a timestamp 29 # string_2_struct = time. strptime ("2016/05/22", "% Y/% m/% d") 30 # print (string_2_struct) 31 # struct_2_stamp = time. mktime (string_2_struct) 32 # print (struct_2_stamp) 33 34 # convert the timestamp to the string format 35 # convert the utc timestamp to the struct_time format 36 # print (time. gmtime (time. time ()-86640) 37 # convert the utc struct_time format to the specified string format 38 # print (time. strftime ("% Y-% m-% d % H: % M: % S", time. gmtime () 39 40 # print (time. strftime ("% Y-% m-% d", time. localtime () 41 42 # Return 12:47:03. 94192543 # print (datetime. datetime. now () 44 45 # The timestamp is directly converted to the date format 2016-08-1946 # print (datetime. date. fromtimestamp (time. time () 47 # current time + 3 days 48 # print (datetime. datetime. now () + datetime. timedelta (3) 49 # current time-3 days 50 # print (datetime. datetime. now () + datetime. timedelta (-3) 51 # current time + 3 hours 52 # print (datetime. datetime. now () + datetime. timedelta (hours = 3) 53 # current time + 30 minutes 54 # print (datetime. datetime. now () + datetime. timedelta (minutes = 30) 55 56 # Time replacement 57 # print (datetime. datetime. now (). replace (minute = 3, hour = 2 ))
View Code

 

2. random Module

1 import random 2 3 # randomly print a decimal number 4 # print (random. random () 5 # randomly print the number between x-y 6 # print (random. randint () 7 # does not contain the following value 8 # print (random. randrange (100, 2) 9 #5 randomly selected 10 # print (random. sample (range (100), 5) 11 12 13 14 import string15 str = string. ascii_letters + string. digits16 print (random. sample (str, 4) 17 18 # checkcode = "19 # for I in range (4): 20 # current = random. randrange (0, 4) 21 # if current! = I: 22 # temp = chr (random. randint (65,90) 23 # else: 24 # temp = random. randint (0, 9) 25 # checkcode + = str (temp) 26 # print (checkcode)
View Code

 

3. shutil Module

1 import shutil 2 3 # copy the file content to another file. Part of the content can be 4 # f1 = open ("1.py") 5 # f2 = open (" mongonew.txt ", "w") 6 # shutil. copyfileobj (f1, f2) 7 8 # copy files and permissions 9 # shutil. copy ("1.py"," mongonew.py ") 10 11 # copy a file 12 # shutil. copyfile ("1.py"," mongonew.py ") 13 14 # copy permission. The target file must have 15 # shutil. copymode ("1.py"," 5.py") 16 17 # copy status information, including: mode bits, atime, mtime, flags18 # shutil. copystat ("1.py"," 5.py") 19 20 # copy file and status information 21 # shutil. copy2 (1.py", "5.py") 2 2 23 # recursively copy a file (directory) 24 # shutil. copytree ("D: \ learn_python3 \ Python basics", "day4") 25 26 # recursively delete a file 27 # shutil. rmtree ("day4") 28 29 # recursively move the file 30 # shutil. move ("day4", "day5") 31 32 # create a compressed package and return the file path 33 # base_name: the file name of the compressed package. It can also be the path of the compressed package. Only when the file name is used, it is saved to the current directory; otherwise, it is saved to the specified path. For example, 34 # Save www => to the current path 35 # For example: /Users/wupeiqi/www => Save to/Users/wupeiqi/36 # format: zip, tar, bztar ", "gztar" 37 # root_dir: Path of the folder to be compressed (default current directory) 38 # owner: User, default: 39 # group: group, default: 40 # logger: used to record logs, usually logging. logger object 41 # shutil. make_archive (base_name = "test", format = "zip", root_dir = r "D: \ learn_python3 \ functions and common modules \ day4", base_dir = "test ") 42 43 # import zipfile44 #45 ## compression 46 # z = zipfile.ZipFile('laxi.zip ', 'w') 47 # z. write ('a. log') 48 # z. write ('data. data ') 49 # z. close () 50 #51 # decompress 52 # z = zipfile.ZipFile('laxi.zip ', 'R') 53 # z. extractall () 54 # z. close () 55 56 # import tarfile57 #58 # compress 59 # tar = tarfile.open('your.tar ', 'w') 60 # tar. add ('/Users/wupeiqi/PycharmProjects/bbs2.zip', arcname='bbs2.zip ') 61 # tar. add ('/Users/wupeiqi/PycharmProjects/cmdb.zip', arcname='cmdb.zip ') 62 # tar. close () 63 #64 # decompress 65 # tar = tarfile.open('your.tar ', 'R') 66 # tar. extractall () # You can set the decompression address 67 # tar. close ()
View Code

 

4. shelve Module

 1 import shelve 2  3 # d = shelve.open("test") 4 # def stu_data(name,age): 5 #     print("register stu",name,age) 6 # name = ["Jack","Tom"] 7 # d["test"] = name 8 # d["func"] = stu_data 9 10 # f = shelve.open("test")11 # print(f["test"])12 # print(f["func"])
View Code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.