温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python中怎么调用zip命令

发布时间:2021-07-05 16:37:03 来源:亿速云 阅读:156 作者:Leah 栏目:编程语言

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' 

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI