重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Python中怎么调用zip命令

Python中怎么调用zip命令,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

成都创新互联是一家集网站建设,五河企业网站建设,五河品牌网站建设,网站定制,五河网站建设报价,网络营销,网络优化,五河网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

Python调用zip命令例子程序是这样的:

  1. #!/usr/bin/Python  

  2. # Filename: backup_ver1.py  

  3. import os  

  4. import time  

  5. # 1. The files and directories to be backed up are specified in a list.  

  6. source = ['/home/swaroop/byte', '/home/swaroop/bin']  

  7. # If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] 
    or something like that  

  8. # 2. The backup must be stored in a main backup directory  

  9. target_dir = '/mnt/e/backup/' # Remember to change this to what 
    you will be using  

  10. # 3. The files are backed up into a zip file.  

  11. # 4. The name of the zip archive is the current date and time  

  12. target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'  

  13. # 5. We use the zip command (in Unix/Linux) to put the files 
    in a zip archive  

  14. zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))  

  15. # Run the backup  

  16. if os.system(zip_command) == 0:  

  17. print 'Successful backup to', target  

  18. else:  

  19. print 'Backup FAILED' 

由于上面Python调用zip命令例子是在Unix/Linux下的,需要改成windows

  1. #!/usr/bin/Python  

  2. # Filename: backup_ver1.py  

  3. import os  

  4. import time  

  5. source =[r'C:\My Documents', r'D:\Work']  

  6. target_dir = r'F:\back up\' # Remember to change this to 
    what you will be using  

  7. target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'  

  8. zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))  

  9. # Run the backup  

  10. if os.system(zip_command) == 0:  

  11. print 'Successful backup to', target  

  12. else:  

  13. print 'Backup FAILED' 

问题一:

当改好后,运行会发生异常,提示:"EOL while scanning single-quoted string",该异常出现在上面代码的粗体行

target_dir = r'F:\back up\'

在Python调用zip命令中,发生错误主要是因为转义符与自然符号串间的问题,看Python的介绍:

  • Python实现tab文件操作相关应用方式解读

  • 使用Python递归对文件进行相关处理

  • Python文件操作简单示例剖析

  • Python数字类型具体含义及应用特点分析

  • Python运算符基本类型总结

自然字符串

如果你想要指示某些不需要如转义符那样的特别处理的字符串,那么你需要指定一个自然字符串。自然字符串通过给字符串加上前缀r或R来指定。例如r"Newlines are indicated by /n"。

如上所说, target_dir的值应该被视作 'F:\back up\',可是这里的转义符却被处理了。如果换成 r'F:\\back up\\' 转义符却没被处理,于是target_dir的值变为'F:\\back up\\'.将单引号变成双引号,结果还是如此。而如果给它加中括号【】,变成【r'F:\back up\'】,则程序又没问题...

于是,解决方法有2个:1)如上所说,加中括号;2)不使用前缀r,直接用转义符‘\’,定义变成target_dir = 'F:\\back up\\'.

问题二:

解决完问题一后,运行module,会提示backup fail. 检查如下:

1. 于是试着将source和target字符串打印出来检验是否文件路径出错,发现没问题

2. 怀疑是windows没有zip命令,在命令行里打‘zip’, 却出现提示帮助,证明可以用zip命令,而且有参数q,r;

3. 想起sqlplus里命令不接受空格符,于是试着将文件名换成没空格的, module成功运行...

现在问题换成如何能让zip命令接受带空格路径,google了一下,看到提示:“带有空格的通配符或文件名必须加上引号”

于是对 zip_command稍做修改,将

zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))

改成:

zip_command = "zip -qr \"%s\" \"%s\"" % (target, '\" \"'.join(source))

改后,module成功运行...

正确的script应为:

  1. #!/usr/bin/Python  

  2. # Filename: backup_ver1.py  

  3. import os  

  4. import time  

  5. source =[r'C:\My Documents', r'D:\Work']  

  6. target_dir = 'F:\\back up\\' # Remember to change this to what 
    you will be using  

  7. target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'  

  8. zip_command = "zip -qr \"%s\" \"%s\"" % (target, ' '.join(source))  

  9. # Run the backup  

  10. if os.system(zip_command) == 0:  

  11. print 'Successful backup to', target  

  12. else:  

  13. print 'Backup FAILED' 

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。


分享题目:Python中怎么调用zip命令
链接地址:http://cqcxhl.com/article/ieghoc.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP