成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Python調用zip命令正確操作方法解析

開發 后端
我們今天將會在這里為大家詳細介紹一下有關Python調用zip命令的操作方法,以及在實際使用出現問題的解決方法,希望可以給大家帶來些幫助。

當我們在應用Python編程語言進行程序開發的時候,我們會發現這一語言可以幫助我們輕松的完成一些特定的功能需求。在這里我們就先一起來了解一下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",該異常出現在上面代碼的粗體行

  1. target_dir = r'F:\back up\' 

在Python調用zip命令中,發生錯誤主要是因為轉義符與自然符號串間的問題,看Python的介紹:#t#

自然字符串

如果你想要指示某些不需要如轉義符那樣的特別處理的字符串,那么你需要指定一個自然字符串。自然字符串通過給字符串加上前綴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命令的相關介紹。

責任編輯:曹凱 來源: 博客園
相關推薦

2010-03-04 14:32:24

Python自動下載文

2010-02-23 17:59:52

WSIT連接WCF

2010-01-28 14:01:32

Android監聽通話

2010-02-01 09:40:08

Python操作

2010-03-03 17:10:57

Python操作Sql

2010-01-27 14:08:56

Android查詢聯系

2009-08-18 15:49:19

C# 操作Excel

2010-03-05 13:48:24

Python for

2010-03-04 09:58:32

安裝Python

2010-02-03 10:23:47

C++操作符重載

2010-02-04 15:05:00

C++ cpuid指令

2009-12-15 13:59:42

Ruby對象操作

2010-03-15 15:18:23

Python運行

2010-02-02 13:57:31

C++解析#pragm

2010-01-28 16:14:33

Android安裝卸載

2009-12-31 11:35:20

Silverlight

2009-09-18 10:58:31

C#數組操作

2009-12-30 14:28:06

Silverlight

2009-12-30 15:53:28

Silverlight

2010-01-05 15:43:13

.NET Framew
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 特黄特色大片免费视频观看 | 日韩色视频 | 国产精品色 | 国产性网 | 欧美中文字幕一区二区三区亚洲 | 日韩欧美三级电影 | 久久人 | 国产日韩欧美电影 | 另类 综合 日韩 欧美 亚洲 | 亚洲色欧美另类 | 在线成人 | 99视频在线 | 国产精品亚洲第一区在线暖暖韩国 | 永久网站 | 天天久 | 亚洲国产一区二区三区 | 青青久在线视频 | 免费日韩网站 | 91久久精品国产 | 成年人网站在线观看视频 | 天天天久久久 | 久久精品亚洲一区二区三区浴池 | 九九伊人sl水蜜桃色推荐 | 国产成人99久久亚洲综合精品 | 三级在线观看 | 欧美成人自拍视频 | 国产亚洲精品综合一区 | 日韩国产在线 | 精品三区| www九色 | 日韩精品在线播放 | 欧美中文视频 | 亚洲精品欧美一区二区三区 | 日韩中文字幕 | 欧美日韩不卡合集视频 | 久久av影院| 性高湖久久久久久久久3小时 | 久久久久久久久淑女av国产精品 | 日韩中文字幕在线视频 | 99一区二区 | 一区二区三区视频在线 |