python apprentissage intégré dans le module

1, le module de temps

1.1 Temps de fonctionnement de commande de priorité du module

1.1.1 temps

import time

1) Le temps est divisé en trois formats:

① horodatage: de 1970 au nombre actuel de secondes écoulé

Effet: l'intervalle de temps utilisé pour calculer

print(time.time())
② selon un certain format d'affichage du temps: 30/03/2020 11:11:11

Rôle: le temps d'exposition

print(time.strftime('%Y-%m-%d %H:%M:%S %p'))
print(time.strftime('%Y-%m-%d %X'))
temps structuré ③

Action: temps pour l'obtention d'une certaine partie de l'individu

res=time.localtime()
print(res)
print(res.tm_year)
print(res.tm_yday)

1.1.2 datetime

import datetime

print(datetime.datetime.now())
print(datetime.datetime.now() + datetime.timedelta(days=3))
print(datetime.datetime.now() + datetime.timedelta(weeks=1))

1.2 module nécessite le temps de fonctionnement maître

1.2.1 La conversion de format de l'heure

struct_time->时间戳
import time
s_time=time.localtime()
print(time.mktime(s_time))

1.2.2 horodatage -> struct_time

tp_time=time.time()
print(time.localtime(tp_time))

1.2.3 ajouté: heure normale du monde et l'heure locale

print(time.localtime())
print(time.gmtime()) # 世界标准时间,了解
print(time.localtime(333333333))
print(time.gmtime(333333333))

1.2.4 struct_time-> temps de chaîne formatée

s_time=time.localtime()
print(time.strftime('%Y-%m-%d %H:%M:%S',s_time))

print(time.strptime('1988-03-03 11:11:11','%Y-%m-%d %H:%M:%S'))

1.2.5 !!! vraiment besoin de savoir une seule: chaîne de format <------> horodatage

'1988-03-03 11:11:11'+7

format string--->struct_time--->timestamp
struct_time=time.strptime('1988-03-03 11:11:11','%Y-%m-%d %H:%M:%S')
timestamp=time.mktime(struct_time)+7*86400
print(timestamp)

format string<---struct_time<---timestamp
res=time.strftime('%Y-%m-%d %X',time.localtime(timestamp))
print(res)

time.sleep(3)

1.3 connaissance

import time
print(time.asctime())

import datetime
print(datetime.datetime.now())
print(datetime.datetime.utcnow())

print(datetime.datetime.fromtimestamp(333333))

2, le module aléatoire

import random

print(random.random()) #(0,1)----float    大于0且小于1之间的小数
print(random.randint(1, 3))  # [1,3]    大于等于1且小于等于3之间的整数

print(random.randrange(1, 3))  # [1,3)    大于等于1且小于3之间的整数

print(random.choice([111, 'aaa', [4, 5]]))  # 1或者23或者[4,5]

print(random.sample([111, 'aaa', 'ccc','ddd'],2))  # 列表元素任意2个组合

print(random.uniform(1, 3))  # 大于1小于3的小数,如1.927109612082716

item = [1, 3, 5, 7, 9]
random.shuffle(item)  # 打乱item的顺序,相当于"洗牌"
print(item)

Application: codes aléatoires

import random

res=''
for i in range(6):
    从26大写字母中随机取出一个=chr(random.randint(65,90))
    从10个数字中随机取出一个=str(random.randint(0,9))

随机字符=random.choice([从26大写字母中随机取出一个,从10个数字中随机取出一个])
res+=随机字符
import random

def make_code(size=4):
    res=''
    for i in range(size):
        s1=chr(random.randint(65,90))
        s2=str(random.randint(0,9))
        res+=random.choice([s1,s2])
    return res

print(make_code(6))

3, le module OS

import os

Obtenez un certain dossier et tous les sous-dossiers et nommez-le sous-dossier

res=os.listdir('.')
print(res)

size=os.path.getsize(r'/Users/linhaifeng/PycharmProjects/s14/day22/01 时间模块.py')
print(size)



os.remove()  删除一个文件
os.rename("oldname","newname")  重命名文件/目录

Application ---- "" ls / "

os.system("ls /")

Mise à disposition: clé et la valeur doit être une chaîne

os.environ['aaaaaaaaaa']='111'
print(os.environ)

print(os.path.dirname(r'/a/b/c/d.txt'))
print(os.path.basename(r'/a/b/c/d.txt'))


print(os.path.isfile(r'笔记.txt'))
print(os.path.isfile(r'aaa'))
print(os.path.isdir(r'aaa'))

print(os.path.join('a','/','b','c','d'))

Recommander

BASE_DIR=os.path.dirname(os.path.dirname(__file__))
print(BASE_DIR)


BASE_DIR=os.path.normpath(os.path.join(
    __file__,
    '..',
    '..'
))
print(BASE_DIR)

Après python3.5, nous avons lancé un nouveau module pathlib

from pathlib import Path

res = Path(__file__).parent.parent
print(res)


res=Path('/a/b/c') / 'd/e.txt'
print(res)

print(res.resolve())


4, le module sys

import sys
python3.8 run.py 1 2 3

La valeur du paramètre est sys.argv interprète acquis

print(sys.argv)

src_file=input('源文件路径: ').strip()
dst_file=input('目标文件路径: ').strip()

src_file=sys.argv[1]
dst_file=sys.argv[2]

# 判断

with open(r'%s' %src_file,mode='rb') as read_f,\
    open(r'%s' %dst_file,mode='wb') as write_f:
    for line in read_f:
        write_f.write(line)

python3.8 run.py src_file dst_file
print('[%-50s]' %'#')
print('[%-50s]' %'##')
print('[%-50s]' %'###')
import time

res=''
for i in range(50):
    res+='#'
    time.sleep(0.5)
    print('\r[%-50s]' % res,end='')
import time

def progress(percent):
    if percent > 1:
        percent = 1
    res = int(50 * percent) * '#'
    print('\r[%-50s] %d%%' % (res, int(100 * percent)), end='')

recv_size=0
total_size=1025011

while recv_size < total_size:
    time.sleep(0.01) # 下载了1024个字节的数据

recv_size+=1024 # recv_size=2048

# 打印进度条

# print(recv_size)

percent = recv_size / total_size  # 1024 / 333333
progress(percent)


Je suppose que tu aimes

Origine www.cnblogs.com/leilijian/p/12602019.html
conseillé
Classement