首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

关于python中命令行调用解决思路

2012-08-17 
关于python中命令行调用import os,timestime.time()source[E:\\1\\]destF:\\1destdest+\\+time.

关于python中命令行调用
import os,time
s=time.time()
source=['E:\\1\\']
dest='F:\\1'
dest=dest+'\\'+time.strftime('%Y%m%d%H%M%S')+'.zip'
zip_command="WinRAR.exe a -r\\%s\\%s"%(dest,source[0])
r=os.system(zip_command)
if r==0:
  print('back up is successful',dest)
else:
  print('failed',zip_command)
print('==============',time.time() -s)

初学python 今天学习本分文件,但是命令行调用一直有问题,程序可以运行,但是就是无法备份
我单步查了一下 r==1 在网上搜了一下 大部分都是针对linux等的,我用的是windows 按理说应该可以啊
但是每次都failed 求高手指导一下。。O(∩_∩)O谢谢

[解决办法]
楼主在CMD.EXE里手动执行一下WinRAR.exe那一行试试, 能否成功?
[解决办法]
貌似格式化出来是WinRAR.exe a -r\F:\1\E:\1\,后面全黏在一起应该不能跑的吧...
[解决办法]
os.system函数 运行 命令,利用这个函数就好像在 系统 中运行命令一样。即在shell中运行命令——如果命令成功运行,它返回0,否则它返回错误号。
其中以zip_command字符串为重点,它包含我们将要执行的命令。你可以在shell(Linux终端中)运行它,以检验它是否工作。但在DOS提示中不一定能执行,必须安装zip,必须能执行zip命令。在网上浏览的时候发现很多人反映zip中的参数设置也较为麻烦,我也没搞懂。就选择了WinRAR,反正是能达到压缩文件的目的。
以下是以Python3.1为背景的代码,用的压缩软件是WinRAR
---------------------------------------------------------------
#Filename:backup_ver1.py
import os
import time
#1.The files and directories to be backed up are specified in alist.
source = r'D:\p'
#2.The backup must be stored in a main backup directory
target_dir = 'D:\\'
#3.The files are backed up into a rar file.
#4.The name of the rar archive is the current date and time
target = target_dir + time.strftime('%Y%m%d%H%M%S')+'.rar'
#5.We use the rar command to put the files in a zip archive
rar_command = 'rar a {0} {1}'.format(target,source)
#Run the backup_ver1
if os.system(rar_command) ==0:
print("Successful backup to",target)
else:
print("Backup FAILED")
---------------------------------------------------------------
操作步骤:
当然啦!你肯定得安装WinRAR啦!
1.将C:\Program Files\WinRAR下的Rar.exe拷贝到%SystemRoot%\system32下,这样你就不必设置rar的环境变量,而能直接再cmd 命令提示符下使用rar命令。
(我的WinRAR路径: C:\Program Files\WinRAR\WinRAR.exe )
2.在命令提示符中测试rar_command命令,能执行则以上代码就没有问题了
[解决办法]
2楼正解,转换后的命令错误,怎么能执行呢?
建议楼主把 zip_command 变量打印出来,手工通过cmd命令行执行下验证
[解决办法]

探讨

2楼正解,转换后的命令错误,怎么能执行呢?
建议楼主把 zip_command 变量打印出来,手工通过cmd命令行执行下验证

[解决办法]
仔细分析了一下LZ的意思,是不是想压缩备份一个文件夹到另外一个文件夹?
写了一个类,经测试,是可以行,希望对LZ有帮助。

Python code
'''Created on 2012-4-14@author: obullxl@gmail.com'''class ZipFile(object):    '''    zip and backup a directory.    '''    def __init__(self, srcDirectory, dstDirectory, zipFileName):        self._srcDirectory = srcDirectory;        self._dstDirectory = dstDirectory;        self._zipFileName = zipFileName;            def archive_backup(self):        import shutil;        # 1.zip        base_name = shutil.make_archive(self._zipFileName, "zip", root_dir=self._srcDirectory);        print(base_name);                if not os.path.exists(self._dstDirectory):            os.makedirs(self._dstDirectory);                # 2.copy        shutil.copy(base_name, self._dstDirectory);                # 3.delete        os.remove(base_name);if __name__ == "__main__":    import os, time    srcDir = os.path.join("C:\\", "Intel");        name = time.strftime('%Y%m%d%H%M%S');    dstDir = os.path.join("F:\\", "zipfile");        zip_file = ZipFile(srcDir, dstDir, name);    zip_file.archive_backup(); 


[解决办法]
没可用分了,灌水,毋怪。
[解决办法]

探讨
...还有就是为什么我把 winrar 放在环境变量中 还不能全局使用呢 谢谢

热点排行