Python基礎-模塊

模塊

模塊有三種python

1.標準模塊,不須要單獨安裝,python自帶模塊
2.第三方模塊,別人寫好,使用得安裝
3.本身寫的python文件
import random
print(random.randint(10000,99999)) #隨機取一個整數
print(random.uniform(1,900)) #取一個小數
stus = ['xiaojun','hailong','yangfan','tanailing','yangyue','cc']
print(random.choice('abcdefg')) #隨機取一個元素
print(random.sample(stus,2)) #隨機取N個元素

l = list(range(1, 101))
print("洗牌以前", l)
print(random.shuffle(l)) #洗牌,這個只能傳list
print("洗牌以後的", l)

做業git

一、寫一個函數,函數的功能是生成一批密碼,存到文件裏面
    def gen_password(num):
        #num表明生成多少條密碼
        pass
    二、密碼複雜度要求
        一、長度在,8-16位之間
        二、密碼必須包括大寫字母、小寫字母、數字、特殊字符
        三、密碼不能重複
    三、生成的密碼保存到文件裏面
二、寫一個函數,函數的功能生成一批雙色球號碼
    def gen_seq(num):
        pass
    一、中獎號碼由6個紅色球號碼和1個藍色球號碼組成。
        紅球的範圍是 1-33
        籃球的範圍是:1-16
    二、產生的不能重複
        籃球: 05  紅球: 01 03 05 17 18 32
        籃球: 05  紅球: 01 03 05 17 18 32
        籃球: 05  紅球: 01 03 05 17 18 32
        籃球: 05  紅球: 01 03 05 17 18 32
#做業1

import random
import string

def gen_passwords1():
    pwd_len = random.randint(8,16)
    upper = random.sample(string.ascii_uppercase,1)
    lower = random.sample(string.ascii_lowercase,1)
    digit = random.sample(string.digits,1)
    punctuation = random.sample(string.punctuation,1)
    other = random.sample(string.ascii_letters+string.digits+string.punctuation,pwd_len-4)
    res = upper+lower+digit+punctuation+other
    random.shuffle(res)
    return ''.join(res)

def gen_password2():
    pwd_len = random.randint(8,16)
    all_str = string.ascii_letters+string.digits+string.punctuation
    res = set(random.sample(all_str,pwd_len))
    if res & set(string.ascii_uppercase) and res & set(string.digits) \
            and res & set(string.ascii_lowercase) and res &  set(string.punctuation):
        return ''.join(res)
    return gen_password2()

all_passwords = set()
num = int(input("請輸入要產生多少條密碼:").strip())
while len(all_passwords) != num:
    res = gen_passwords1()+'\n'
    all_passwords.add(res)

with open('passwords.txt','w',encoding='utf-8') as fw:
    fw.writelines(all_passwords)
#列表生成式
#res = ['01','02','03','33']
#res = []
# for i in range(1,34):
#     res.append(str(i).zfill(2))
l = [i for i in range(10)] #先寫for i in range(10),把生成的i放前面再寫
print(l)
res = [str(i).zfill(2) for i in range(1,34)]
print(res)
#列表生成式
import random
def gen_seq():
    all_red_ball = [str(i).zfill(2) for i in range(1,34)]
    all_blue_ball = [str(i).zfill(2) for i in range(1,17)]
    blue = random.choice(all_blue_ball)
    red = random.sample(all_red_ball,6)
    red = ''.join(red)
    return '紅球:%s 藍球:%s'%(red, blue)

all_seq = set()
num = int(input("請輸入要產生多少條密碼:").strip())
while len(all_seq) != num:
    res = gen_seq()+'\n'
    all_seq.add(res)

with open('seq.txt','w',encoding='utf-8') as fw:
    fw.writelines(all_seq)

經常使用模塊

os模塊apache

import os
os.rename(old, new) #重命名
os.remove(f) #刪除文件
os.mkdir('china/beijing') #建立文件夾,父目錄存在才能建立下級目錄
os.makedirs('china/beijing') #父目錄不存在會自動建立
os.removedirs('china') #只能刪除空文件夾

print(os.listdir("e:\\")) #顯示該目錄下的全部文件和文件夾
print(os.path.isdir('E:\\apache-jmeter-3.3')) #判斷是不是文件夾
print(os.path.isfile('e:\\2018年度項目工做總結及規劃.pptx')) #判斷是否爲文件
print(os.path.exists('E:\\apache-jmeter-3.3')) #判斷文件或文件夾是否存在
res1 = os.system('dir') #執行操做系統命令
res2 = os.popen('ipconfig').read() #執行系統操做命令後並返回值
print(res2)

os.path.join('china','a.py') #拼接路徑
os.path.split(r'D:\China\beijing\a.py') #把路徑和文件名分割開
os.path.dirname(r'D:\China\beijing\a.py') #取父目錄
os.path.getsize(r'D:\China\beijing\a.py') #取文件大小,單位是字節
os.getcwd() #取當前目錄
os.chdir(r'D:\China\beijing') #進入到哪一個目錄
os.path.getatime() #獲取文件時間
res = os.walk('D:\\') #遍歷全部目錄和文件
count = 0
for cur_path,dirs,files in res:
    #print('文件:',files,'當前目錄:',cur_path,'文件夾',dirs)
    for f in files:
        if f.endswith('py'):
            #count += 1
            os.remove(os.path.join(cur_path,f))
print(count)

def find_file(path, keyword):
    #查找文件的
    res = os.walk(path)
    for cur_path, dirs, files in res:
        for file_name in files:
            if keyword in file_name:
                print("該文件在%s下面" %cur_path)
                
find_file("D:\\","設備部接口新.jmx")
相關文章
相關標籤/搜索