sys模块

sys模块内容

sys.argv  # 命令行参数List,第一个参数是程序本身路径
sys.exit(n)  # 退出程序,正常退出时exit(0)
sys.version  # 获取python解释程序的版本信息
sys.maxint  # 最大的Int值
sys.path  # 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.plarform  # 返回操作系统平台名称

1.应用于文件拷贝

1.1旧方法

ori_file = input('原文件路径')
tar_file = input('目标文件路径')

with open(r'%s' % ori_file, mode='rb') as f_read, \
        open(r'%s' % tar_file, mode='wb') as f_write:
    for line in f_read:
        f_write.write(line)

1.2新方法

import sys

ori_file = sys.argv[1]
tar_file = sys.argv[2]

with open(r'%s' % ori_file, mode='rb') as f_read, \
        open(r'%s' % tar_file, mode='wb') as f_write:
    for line in f_read:
        f_write.write(line)

# 在run.py所在的文件夹下,按住shift,右键选择“在此处打开power shell”,输入所需内容
# 格式:python3 run.py 原文件路径 新文件路径 注:argv[0]为run.py路径

2.进度条打印的应用

def func(file_load, down_load=0):
    def progress_bar(percent):
        bar = int(percent * 50) * '#'
        print('\r[%-50s] %d%%' % (bar, percent * 100), end='')

    while down_load < file_load:
        percent = down_load / file_load
        down_load += 1024
        time.sleep(0.3)
        if percent > 1:
            percent = 1
        progress_bar(percent)

 

posted @ 2020-12-26 14:32  Avery_W  阅读(43)  评论(0编辑  收藏  举报