windows 下python程序调用rar命令

Kunz_sunningup 2017-03-29 08:42:33
我仿照写了个备份python,在网上找了很多方法但仍不成功:
#!/usr/bin/python
# Filename: backup_ver1.py

import os
import time

# 1. The files and directories to be backed up are specified in a list.
source = r'D:\doc\doc'
# If you are using Windows,
# use source = [r'C:\Documents', r'D:\Work'] or something like that

# 2. The backup must be stored in a main backup directory
##target_dir = '/mnt/e/backup/' # Remember to change this to what you will be using
target_dir = 'D:\\backup\\'

# 3. The files are backed up into a zip file.
# 4. The name of the zip archive is the current date and time
target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.rar'

# 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
rar_command = r'"C:\Program Files\WinRAR\WinRAR.exe" a %s %s -r' % (target,source)

# Run the backup
print '\nRun the backup:',
print rar_command+'\n'
if os.system(rar_command) == 0:
print 'Successful backup to', target
else:
print 'Backup FAILED'

将target_dir = 'D:\\backup\\'这一行改为target_dir = 'D:\\backup' 可以运行成功,但文件路径就不是D:\backup\time 而是成为D:\backuptime
而且根本找不到备份好的文件。
求大神指导!!
...全文
768 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
XNLBcx 2018-09-06
  • 打赏
  • 举报
回复
将target_dir = 'D:\\backup\\'改为target_dir = 'D:\\backup'
再将target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.rar'改为target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.rar'
试试看呢。
CDSoftwareWj 2017-04-04
  • 打赏
  • 举报
回复
你调试一下 看看target改完以后的值,程序你都写出来了,看看值你就知道问题出在那里了
Kunz_sunningup 2017-04-01
  • 打赏
  • 举报
回复
引用 3 楼 CDSoftwareWj 的回复:
这句

target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.rar'
改为:

target = os.path.normpath('%s/%s.rar' % (target_dir , time.strftime('%Y%m%d%H%M%S')))
我改了以后,都不调用rar.exe了,直接输出Backup failed
chuifengde 2017-03-31
  • 打赏
  • 举报
回复
3.6下测试没问题,你单步调试一下看看
CDSoftwareWj 2017-03-31
  • 打赏
  • 举报
回复
这句

target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.rar'
改为:

target = os.path.normpath('%s/%s.rar' % (target_dir , time.strftime('%Y%m%d%H%M%S')))
Kunz_sunningup 2017-03-30
  • 打赏
  • 举报
回复
怎么没人啊!!求大神啊!!!

37,742

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧