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")
相关文章
相关标签/搜索