python-study-19

随机验证码--random

import random

# 1X3Y3ZX
def make_code(size=7):
    res = ''
    for i in range(size):
        # 循环一次则得到一个随机字符(字母/数字)
        s = chr(random.randint(65, 90))
        num = str(random.randint(0, 9))
        res += random.choice([s, num])
    return res

res=make_code()
print(res)
View Code

打印进度条--格式化字符串

def make_progress(percent,width=50):
    if percent > 1:percent=1
    show_str=('[%%-%ds]' % width) % (int(percent * width) * '#')
    print('\r%s %s%%' %(show_str,int(percent * 100)),end='')

total_size=1025
recv_size=0
while recv_size < total_size:
    time.sleep(0.1) # 模拟经过了0.5的网络延迟下载了1024个字节
    recv_size+=1024
    # 调用打印进度条的功能去打印进度条
    percent=recv_size / total_size
    make_progress(percent)
View Code
'''

[#                  ]
[##                 ]
[###                ]
[####               ]
[#####              ]

'''
# print('[%-50s]' %'#')
# print('[%-50s]' %'##')
# print('[%-50s]' %'###')
# print('[%-50s]' %'####')
# print('[%-50s]' %'#####')

# print('%s%%' %50)

# 1、控制打印进度条的宽度
# res='[%%-%ds]' %50
# print(res %'#')
# print(res %'##')
# print(res %'###')
# print(res %'####')
# print(res %'#####')

#2、不换行+跳回行首打印
# import time
# print(('\r[%%-%ds]' %50) %'#',end='')
# time.sleep(0.5)
# print(('\r[%%-%ds]' %50) %'##',end='')
# time.sleep(0.5)
# print(('\r[%%-%ds]' %50) %'###',end='')
# time.sleep(0.5)
# print(('\r[%%-%ds]' %50) %'####',end='')
# time.sleep(0.5)
# print(('\r[%%-%ds]' %50) %'#####',end='')

# import time
#
# def make_progress(percent,width=50):
#     if percent > 1:percent=1
#     show_str=('[%%-%ds]' % width) % (int(percent * width) * '#')
#     print('\r%s %s%%' %(show_str,int(percent * 100)),end='')
#
# total_size=1025
# recv_size=0
# while recv_size < total_size:
#     time.sleep(0.1) # 模拟经过了0.5的网络延迟下载了1024个字节
#     recv_size+=1024
#     # 调用打印进度条的功能去打印进度条
#     percent=recv_size / total_size
#     make_progress(percent)



#1 控制宽度
# print('[%s]' %'#')
# print('[%-50s]' %'#') #固定宽度
# print('[%-%ds]' %50) #宽度不能固定死 传值错误 解决给%d传值的问题 只让%d有意义 %s取消意义
# print('[%%-%ds]' %50)
# res = '[%%-%ds]' %50  #写活宽度
# print(res %'#')        #基于上一步 在给%s 传值

#2 不换行 和自动跳到行首  end=''   \r \n
# print(('\r[%%-%ds]' %50) %'#',end='')
# print(('\r[%%-%ds]' %50) %'##',end='')
# print(('\r[%%-%ds]' %50) %'###',end='')
详细步骤

re正则模块

"""
1、什么是正则
    正则就是用一系列具有特殊含义的字符组成一套规则,该规则用来描述具有某一特征的字符串,
    正则就是用来去一个大的字符串中匹配出符合规则的子字符串

2、为什么要用正则
    1、用户注册
    2、爬虫程序

3、如何用正则

"""
# import re

# print(re.findall('\w','hello 123_ */-='))
# print(re.findall('\W','hello 123_ */-='))

# print(re.findall('\s','hell\no 12\t3_ */-='))
# print(re.findall('\S','hell\no 12\t3_ */-='))

# print(re.findall('\d','hell\no 12\t3_ */-='))
# print(re.findall('\D','hell\no 12\t3_ */-='))

# print(re.findall('\n','hell\no 12\t3_ */-='))
# print(re.findall('\t','hell\no 12\t3_ */-='))
# print(re.findall('l','hell\no 12\t3_ */-='))


# print(re.findall('egon','my name is egon,egon is beautiful'))
#                                                      egon
# print(re.findall('^egon','egon my name is egon,egon is beautiful'))
# print(re.findall('egon$','egon my name is egon,egon is beautifulegon1'))
#                                                                egon


# 重复匹配
# .:匹配换行符以外的任意一个字符
# print(re.findall('a.c','abc a1c aac asd aaaaac a*c a+c abasd')) #['abc','a1c','aac','aac','a*c','a+c']
#                                                        a.c
# print(re.findall('a.c','abc a1c aac a\nc asd aaaaac a*c a+c abasd',re.DOTALL))

# []:匹配一个字符,该字符属于中括号内指定的字符
# print(re.findall('a..c','abc a1 c aac asd aaaaac a *c a+c abasd ='))
# print(re.findall('a.c','abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c'))
# print(re.findall('a[a-z]c','abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c'))
# print(re.findall('a[A-Z]c','abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c'))
# print(re.findall('a[-+*/]c','abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c'))
# print(re.findall('a[a-z][a-z]c','abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c'))
# print(re.findall('a[^a-z]c','abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c'))


# *: 必须与其他字符连用,代表左侧的字符出现0次或者无穷次
# print(re.findall('ab*','a ab abbb abbbb a1bbbb a-123'))
#                                              ab*
#['a','ab','abbb','abbbb','a','a']
# print(re.findall('ab{0,}','a ab abbb abbbb a1bbbb a-123'))



# ?: 必须与其他字符连用,代表左侧的字符出现0次或者1次
# print(re.findall('ab?','a ab abbb abbbb a1bbbb a-123'))
#                                              ab?
#['a','ab','ab','ab','a','a']
# print(re.findall('ab{0,1}','a ab abbb abbbb a1bbbb a-123'))



# +: 必须与其他字符连用,代表左侧的字符出现1次或者无穷次
# print(re.findall('ab+','a ab abbb abbbb a1bbbb a-123'))
#                                              ab+
# ['ab','abbb','abbbb']
# print(re.findall('ab{1,}','a ab abbb abbbb a1bbbb a-123'))


# {n,m}: 必须与其他字符连用
# print(re.findall('ab{1,3}','a ab abbb abbbb a1bbbb a-123'))
#                                                  ab{1,3}
# ['ab','abbb','abbb']


# .*:贪婪匹配
# print(re.findall('a.*c','ab123adfc1134124123adasfc123123'))

# .*?:非贪婪匹配
# print(re.findall('a.*?c','ab123adfc1134124123adasfc123123'))
#                                            a.*?c


#():分组
# print(re.findall('expression="(.*?)"','expression="1+2+3/4*5" egon="beautiful"'))
#                                       expression=".*?"


# print(re.findall('href="(.*?)"','<p>段落</p><a href="https://www.sb.com">点我啊</a><h1>标题</h1><a href="https://www.sb.com">点我啊</a>'))



#|:
# print(re.findall('a|b','ab123abasdfaf'))
#                        a|b

# print(re.findall('compan(?:ies|y)','Too many companies have gone bankrupt, and the next one is my company'))

#companies   company



# print(re.findall(r'a\\c','a\c a1c aAc aac'))
# print(re.findall('a\\\\c','a\c a1c aAc aac'))

# print(re.findall('ale(x)','alex is SB,alex is bigSB'))
# print(re.search('alex','alex is SB,alex is bigSB'))
# print(re.search('ale(x)','alex is SB,alex is bigSB').group())
# print(re.search('abcdefg','alex is SB,alex is bigSB'))

# print(re.search('^alex','123alex is SB,alex is bigSB'))
# print(re.match('alex','123alex is SB,alex is bigSB'))

# l='egon:18:male'.split(':')
# print(l)
# l1=re.split('[ :/-]','a-b/c egon:18:male xxx')
# print(l1)

# print(re.sub('[a-z]+xx','yxp','lxx is good,sb is lllxx wxx is good cxx is good'))
#                                                   [a-z]+xx

# pattern=re.compile('alex')
# print(pattern.findall('alex is SB,alex is bigSB'))
# print(pattern.search('alex is SB,alex is bigSB'))
View Code

sys模块--cp功能--sys.argv

# import sys

# sys.path

# sys.argv # 用来接收python解释器执行py文件后跟的参数
#例如:python cp.py argv1 argv2 arg3
#sys.argv=['cp.py','argv1','argv2','argv3']
View Code
import sys

# print(sys.argv)
# src_file=input('请输入源文件路径:')
src_file=sys.argv[1]
# dst_file=input('请输入目标文件路径:')
dst_file=sys.argv[2]
with open(src_file,'rb') as read_f,\
    open(dst_file,'wb') as write_f:
    for line in read_f:
        write_f.write(line)
cp功能

subprocess模块---subprocess.Popen

# import os
# os.system('tasklist')

import subprocess
import time

obj=subprocess.Popen(
    'taskliasdfsadfst',
    shell=True,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE

)
# print(obj)
# stdout_res=obj.stdout.read()
# print(stdout_res.decode('gbk'))
# print(stdout_res)

stderr_res1=obj.stderr.read()
stderr_res2=obj.stderr.read()
stderr_res3=obj.stderr.read()
# print(stderr_res1.decode('gbk'))
print(stderr_res1)
print(stderr_res2)
print(stderr_res3)

# import time
# time.sleep(50)
View Code

os.path.normpath

import os
# print(os.path.normcase('c:/WIndows\\system32\\')   )

# print(os.path.normpath('c://windows\\System32\\../Temp/')   )

BASE1_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# print(BASE1_DIR)

res=os.path.normpath(os.path.join(
    os.path.abspath(__file__),
    '..',
    '..'
))
print(res)
View Code

猜你喜欢

转载自www.cnblogs.com/xujinjin18/p/9210535.html