python winrar 密码_python+winrar 指令压缩文件

1 windows环境

2已安装winrar软件

3将winrar.exe拷贝到系统的system32目录下

4压缩的源文件D:/WORK

5目标文件位于D盘根目录下,名称为当前的时间日期.rar

import os

import time

source = r’D:work’

target_dir = r’D:\’

target = target_dir + time.strftime(‘%Y%m%d%H%M%S’)+’.rar’

rar_command = “winrar a {0} {1}”.format(target, source)

if os.system(rar_command) ==0:

print(“成功”)

else:

print(“失败”)

优化方案一、

import os

import time

source = r’D:work’

target_dir = r’D:\’

today = target_dir + time.strftime(‘%Y%m%d’)

now = time.strftime(‘%H%M%S’)

if not os.path.exists(today):

os.mkdir(today) # make directory

print( ‘Successfully created directory’, today)

target = today + os.sep + now + ‘.rar’

rar_command = “winrar a {0} {1}”.format(target, source)

if os.system(rar_command) ==0:

print(“成功”)

else:

print(“失败”)

优化方案二、

import os

import time

source = r’D:work’

target_dir = r’D:\’

today = target_dir + time.strftime(‘%Y%m%d’)

now = time.strftime(‘%H%M%S’)

comment = input(‘Enter a comment –> ‘)

if len(comment) == 0: # check if a comment was entered

target = today + os.sep + now + ‘.rar’

else:

target = today + os.sep + now + ‘_’ +

comment.replace(‘ ‘, ‘_’) + ‘.rar’

if not os.path.exists(today):

os.mkdir(today) # make directory

print( ‘Successfully created directory’, today)

rar_command = “winrar a {0} {1}”.format(target, source)

if os.system(rar_command) ==0:

print(“成功”)

else:

print(“失败”)

文章知识点与官方知识档案匹配,可进一步学习相关知识Python入门技能树首页概览211742 人正在系统学习中 相关资源:专用的软件解决集墨棉使用寿命已尽-专业指导工具类资源-CSDN文库

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年1月17日
下一篇 2021年1月17日

相关推荐