time,datatime,random,os,sys,hashlib模塊

time模塊

文件命名規範:不能夠以模塊名直接對文件命名 例如:time.pypython

在python中的三種表現形式:git

1.時間戳:給電腦看的算法

2.格式化時間(Format String)給人看到app

返回的是時間的字符串 2002-01-11dom

格式化時間對象(struct_time)加密

​ -返回的是一個元組,元組中有9個值:操作系統

​ 9個值分別表明:年月日時分秒,一週中的第幾天,一年中的第幾天,夏令時命令行

時間模塊:code

import time


# 1.獲取時間戳   計算時間的時候使用

print(time.time())

# 2.獲取格式化時間 拼接用戶時間格式並保存使用

# 獲取年月日

print(time.strftime('%Y-%m-%d'))

# 獲取年月日時分秒

print(time.strftime('%Y-%m-%d %H-%M-%S'))

# %X =%H-%M-%S

print(time.strftime('%Y-%m-%d %X'))

# 獲取年月

print(time.strftime('%Y/%m'))



1573885838.594383
2019-11-16
2019-11-16 14-30-38
2019-11-16 14:30:38
2019/11
# 3.獲取時間對象
print(time.localtime())
print(type(time.localtime()))
time_obj = time.localtime()
print(time_obj.tm_yday)
print(time_obj.tm_year)
print(time_obj.tm_hour)


time.struct_time(tm_year=2019, tm_mon=11, tm_mday=16, tm_hour=14, tm_min=40, tm_sec=23, tm_wday=5, tm_yday=320, tm_isdst=0)
<class 'time.struct_time'>
320
2019
14
res = time.localtime()
time.sleep(5)

# 獲取當前哥前時間的格式化時間
print(time.strftime('%Y-%m-%d %X', time.localtime()))
# 將時間對象轉換爲格式化時間
print(time.strftime('%Y-%m-%d %X', res))
# 將字符串格式的時間轉換爲時間對象
res = time.strptime('2019-01-01', '%Y-%m-%d')
print(res)


2019-11-16 14:46:34
2019-11-16 14:46:29
time.struct_time(tm_year=2019, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=1, tm_isdst=-1)

datetime模塊

import  datetime
# 獲取當前年月
print(datetime.date.today())
# 獲取當前年月日時分秒
print(datetime.datetime.today())
time_obj = datetime.datetime.today()
print(type(time_obj))
print(time_obj.year)
print(time_obj.month)
print(time_obj.day)
# UTC
print(time_obj.weekday())   # 0-6
# ISO
print(time_obj.isoweekday())    # 1-7

# UTC時區
# 北京時區
print(datetime.datetime.now())
# 格林威治
print(datetime.datetime.utcnow())

    
5
6
2019-11-16 15:00:43.409280
2019-11-16 07:00:43.409280

日期/時間的計算orm

​ 日期時間 =日期時間 + or - 時間對象

​ 時間對象 =日期時間 +or- 日期時間

# 日期時間:
current_time = datetime.datetime.now()
print(current_time)

# 時間對象
# 獲取七天時間
time_obj = datetime.timedelta(days=7)
print(time_obj)

# 獲取當前時間七天後的時間
# 日期時間 =  日期時間 +or- 時間對象
late_time = current_time + time_obj
print(late_time)

# 時間對象 = 日期時間  + or- 日期時間
time_new_obj = late_time-current_time
print(time_new_obj)


2019-11-16 15:16:06.708308
7 days, 0:00:00
2019-11-23 15:16:06.708308
7 days, 0:00:00

random模塊

import random

# 隨機獲取1-9中的任意的整數
res = random.randint(1, 9)
print(res)
# 默認獲取0-1之間任意小數
res2 = random.random()
print(res2)
# 洗牌
# 將可迭代對象中的值進行亂序(只能是集合)
my_list = [1, 2, 3, 4]
# print(random.shuffle(my_list))
random.shuffle(my_list)
print(my_list)
# 隨機獲取可迭代對象中的某一個值
list1 = [1, 2, 3, 4, 5]
print(random.choice(list1))

6
0.32282242064458144
[1, 2, 4, 3]
5

需求: 隨機驗證碼
'''
需求:
大小寫字母、數字組合而成
組合5位數的隨機驗證碼

前置技術:

chr(97) # 能夠將ASCII表中值轉換成對應的字符

print(chr(101))

random.choice
'''

# 隨機位數的驗證碼
def get_code(num):
    code = ''
    # 每次循環只從大小寫字母,數字中取出一個字符
    for line in range(num):
        # 先隨機獲取一個小寫字母
        res1 = random.randint(97, 122)

        lower_str = chr(res1)

        # 隨機獲取一個大寫字母
        res2 = random.randint(65, 90)

        upper_str = chr(res2)

        # 隨機獲取一個數字
        res3 = random.randint(0, 9)

        number = str(res3)

        code_list = [lower_str, upper_str, number]

        random_code = random.choice(code_list)

        code += random_code

    return code


code = get_code(10)
print(code)


MI1If73hAK

os模塊

os與操做系統交互的模塊

import os

# 需求:獲取當前項目根目錄

# 獲取當前文件中的上一級目錄
DAY15_PATH = os.path.dirname(__file__)
print(DAY15_PATH)

# 項目的根目錄,路徑相關的值都用‘常量’   ps
# BASE_PATH = os.path.dirname(DAY15_PATH)
# print(BASE_PATH)
BASE_PATH = os.path.dirname(os.path.dirname(__file__))
print(BASE_PATH)


# 路徑的拼接: 拼接文件   ‘絕對路徑’
TEST_PATH = os.path.join(DAY15_PATH, '大帥的寫真集.txt')
print(TEST_PATH)

# 判斷‘文件/文件夾是否存在: 若文件存在則返回True,若不存在則返回False’
print(os.path.exists(TEST_PATH))
print(os.path.exists(DAY15_PATH))
print(os.path.exists(r'F:\python_work\python_oldboyedu_learn\day15\os模塊.py'))

# 單獨判斷‘文件夾’是否存在,只能判斷‘文件夾’,無法判斷‘文件’
print(os.path.isdir(DAY15_PATH))
print(os.path.isdir(TEST_PATH))
print(os.path.isdir(r'F:\python_work\python_oldboyedu_learn\day15\os模塊.py'))

# 建立文件夾
DIR_PATH = os.path.join(r'F:\python_work\python_oldboyedu_learn\day15', '大帥的寫真集')
# os.mkdir(DIR_PATH)

# 刪除文件夾:只能刪除‘空的文件夾’
# os.rmdir(DIR_PATH)

# 獲取某個文件夾中全部文件的名字
DSB_list = os.listdir(r'F:\python_work\python_oldboyedu_learn\day15\大帥的寫真集')
print(DSB_list)

# enumerate(可迭代對象) ————————》獲得一個對象,對象有一個個的元組(索引,元素)
res = enumerate(DSB_list)
print(list(res))
# 需求:打印全部的做品,讓用戶自行選擇編號,講結果打印出來
while True:
    # 打印出全部的編號文件
    for index, name in enumerate(DSB_list):
        print(f'編號:{index} 文件名:{name}')
        # 讓用戶選擇編號
    choice = input('請選擇想看的做品 選擇編號:').strip()
    # 做出判斷,限定用戶必須輸入的是數字,數字的範圍要在編號範圍內
    if not choice.isdigit():
        print('您必須輸入數字')
        continue

    choice = int(choice)
    if choice not in range(len(DSB_list)):
        print('編號範圍錯誤!')
        continue

    file_name = DSB_list[choice]
    DSB_path = os.path.join(r'F:\python_work\python_oldboyedu_learn\day15\大帥的寫真集', file_name)

    with open(DSB_path, 'r', encoding='utf-8') as f:
        print(f.read())
        break
        
編號:0 文件名:__init__.py
編號:1 文件名:唐嫣寫真集
編號:2 文件名:唐藝昕寫真集.txt
編號:3 文件名:張歆藝寫真集
請選擇想看的做品 選擇編號:2
唐藝昕大寶貝
超愛唐藝昕
唐藝昕真妹妹

sys模塊

import sys
import os

# 獲取當前的python解釋器的環境變量路徑
print(sys.path)

# 將當前項目添加到環境變量中
BASE_PATH = os.path.dirname(os.path.dirname(__file__))
sys.path.append(BASE_PATH)
print(sys.path)

# 獲取cmd終端的命令行 python3 py文件 用戶名 密碼
print(sys.argv)  # 返回的是列表




['F:\\python_work\\python_oldboyedu_learn\\day15', 'F:\\python_work\\python_oldboyedu_learn', 'F:\\PyCharm 2019.2.3\\helpers\\pycharm_display', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36\\python36.zip', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36\\DLLs', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36\\lib', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages', 'F:\\PyCharm 2019.2.3\\helpers\\pycharm_matplotlib_backend']
['F:\\python_work\\python_oldboyedu_learn\\day15', 'F:\\python_work\\python_oldboyedu_learn', 'F:\\PyCharm 2019.2.3\\helpers\\pycharm_display', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36\\python36.zip', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36\\DLLs', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36\\lib', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages', 'F:\\PyCharm 2019.2.3\\helpers\\pycharm_matplotlib_backend', 'F:/python_work/python_oldboyedu_learn']
['F:/python_work/python_oldbolibyedu_learn/day15/sys模塊.py']

hashlib模塊

hashlib 是一個加密模塊:

​ 內置了不少算法

​ -md5 :不可解密的算法(2018年之前)

摘要算法:

​ -摘要是從某個內容中獲取的加密字符串

​ -摘要同樣,內容就必定同樣:保證惟一性

​ -密文密碼就是一個摘要

import hashlib

md5_obj = hashlib.md5()
# print(type(md5_obj))
# print(md5_obj)
str1 = '1234'
# update中必定要傳入bytes類型數據
md5_obj.update(str1.encode('utf-8'))

res = md5_obj.hexdigest()
print(res)

81dc9bdb52d04dc20036dbd8313ed055
# 以上操做撞庫有可能會破解真實密碼
# 防止撞庫的問題:加鹽

import hashlib


def pwd_md5(pwd):
    md5_obj = hashlib.md5()
    str1 = pwd
    md5_obj.update(str1.encode('utf-8'))

    # 創造鹽

    sal = '唐藝昕我超級喜歡你哦'
    md5_obj.update(sal.encode('utf-8'))

    # 就能獲得一個加密的字符串
    res = md5_obj.hexdigest()
    
    return res


# 模擬用戶登陸操做

# 獲取文件中的用戶名和密碼
with open('user.txt', 'r', encoding='utf-8') as f:
    user_str = f.read()

file_user, file_pwd = user_str.split(':')

# 用戶輸入用戶名和密碼
username = input('請輸入用戶名:').strip()
password = input('請輸入密碼:').strip()

# 校驗用戶名與密碼是否一致
if username == file_user and file_pwd == pwd_md5(password):
    print('登陸成功!')
else:
    print('登陸失敗!')
    if username != file_user:
        print('用戶名不正確!')
    if file_pwd != pwd_md5(password):
        print('密碼不正確!')
        
user.txt中的密碼爲:godlover:0291433999dba37a6e76e88802246fd8('1234'+'唐藝昕我超級喜歡你哦')


請輸入用戶名:godlover
請輸入密碼:1234
登陸成功!

請輸入用戶名:godlover
請輸入密碼:123456
登陸失敗!
密碼不正確

請輸入用戶名:godlover12
請輸入密碼:1234
登陸失敗!
用戶名不正確!

請輸入用戶名:godloverwq
請輸入密碼:45456
登陸失敗!
用戶名不正確!
密碼不正確!
相關文章
相關標籤/搜索