2015-10-27 46 views
0

我想解壓文件python。我給我的程序一個文件路徑,該文件的主目錄有9個子目錄和python循環遍歷它們,找到具有特定名稱的tar文件並對它們進行解壓縮。使用python的Untar文件沒有錯誤/警告?

所以我output_text文件有我的主目錄,i循環之後throught在該目錄中我01-09文件的數量和解壓文件與他們特定名稱。

我已經寫了一些代碼,但發生的事情是什麼都沒有,沒有錯誤,沒有警告什麼

def main(): 
    output_path = "/Users/rs/Documents/clients_file.txt" 
    path = [] 
    with open(output_path) as f: 
     for exptini_path_raw in f: 
      exptini_path = exptini_path_raw.strip() 
      path.append(exptini_path) 

for i in range(1,1): 
    for j in range(0,len(path)): 
     if i < 10: 
      p = "/%s/0%d/middleware"%(path[j],i) 
     else: 
      p = "/%s/%d/middleware"%(path[j],i) 
    for root, _, files in os.walk(p): 
      for f in files: 
       if not 'client-logs' or 'middleware-logs' in f: 
        continue 
       print 'going to extract %s'%f 
       f1 = os.path.join(p,f) 
       tar = tarfile.open(f1) 
       tar.extractall() 
       tar.close() 



if __name__ == '__main__': 
    main() 

注:即使我打印出不出現,可能是什麼原因?

編輯:我修改了代碼,現在得到這個

File "unzip_files.py", line 47, in <module> 
    main() 
    File "unzip_files.py", line 40, in main 
    tar = tarfile.open(f1) 
    File 

「/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tarfile.py」,行1672在開放 提高ReadError(「文件不能成功打開」) tarfile.ReadError:文件不能成功打開

這是我的mdodified代碼:

進口OS 進口os.path中 import tarfile

def main(): 
    output_path = "/Users/rs/Documents/clients_file.txt" 
    path = [] 
    with open(output_path) as f: 
     for exptini_path_raw in f: 
      exptini_path = exptini_path_raw.strip() 
      path.append(exptini_path) 
      print path 

for i in range(1,10): 
    print i 
    for j in range(0,len(path)): 
     print j 
     if i < 10: 
      p = "/%s/0%d"%(path[j],i) 
     else: 
      p = "/%s/%d"%(path[j],i) 
     print p 
     for root, _, files in os.walk(p): 
      for f in files: 
       if not 'client-logs' or 'middleware-logs' in f: 
        continue 
       print 'going to extract %s'%f 
       f1 = os.path.join(p,f) 
       tar = tarfile.open(f1) 
       tar.extractall() 
       tar.close() 



if __name__ == '__main__': 
    main() 

注意:該文件是一個tgz文件!

回答

1

for i in range(1,1):什麼都不做。你不會得到任何迭代。

試試看:

>>> for i in range(1,1): 
...  print i 
... 
>>> for i in range(1,2): 
...  print i 
... 
1 

我想你想要更多的東西是這樣的:

編輯:我已經修復並測試了以下內容。

untarscript.py

import os, tarfile 

def main(): 
    output_path = "/Users/rs/Documents/clients_file.txt" 
    path = [] 
    with open(output_path) as f: 
     for exptini_path_raw in f: 
      exptini_path = exptini_path_raw.strip() 
      print "Adding to path: {}".format(exptini_path) 
      path.append(exptini_path) 

    for i in range(1,10): 
     for j in range(0,len(path)): 
      p = "{}/{:0>2}/middleware".format(path[j], i) 
      print "Path to search for tar files: {}".format(p) 
      for root, dirs, files in os.walk(p): 
       for f in files: 
        print "Investigating file: {}".format(f) 
        if not ('client-logs' in f or 'middleware-logs' in f): 
         print "This file does not match expected file name...skipping: {}".format(f) 
         continue 
        print 'going to extract {} to folder {}'.format(os.path.join(p,f), p) 
        f1 = os.path.join(p,f) 
        tar = tarfile.open(f1) 
        tar.extractall(path=p) 
        tar.close() 

if __name__ == '__main__': 
    main() 

for i in range(1,10):會給你子目錄號碼1 - 9

{:0>2}將i的值和0墊它是2位數字。刪除了%並使用較新的format string syntax

修復for root, dir, files in os.walk(p):行的掛鉤,以便在每次更新p時執行該掛鉤。

代替if not 'client-logs' or 'middleware-logs' in f:if not ('client-logs' in f or 'middleware-logs' in f):,因爲middleware-logs的測試失敗。

path=p參數添加到tar.extractall()以確保tar文件被提取到找到它的同一文件夾。如果它不是所需的行爲,可以將其刪除。

執行untarscript.py前:

[[email protected] Documents]# pwd 
/Users/rs/Documents 
[[email protected] Documents]# ls -al 
total 24 
drwxr-xr-x 5 root root 4096 Oct 27 15:09 . 
drwxr-xr-x 5 root root 4096 Oct 27 15:09 .. 
-rw-r--r-- 1 root root 87 Oct 27 12:30 clients_file.txt 
drwxr-xr-x 11 root root 4096 Oct 27 12:37 exptini1 
drwxr-xr-x 11 root root 4096 Oct 27 12:37 exptini2 
drwxr-xr-x 11 root root 4096 Oct 27 12:37 exptini3 
[[email protected] Documents]# cat clients_file.txt 
/Users/rs/Documents/exptini1 
/Users/rs/Documents/exptini2 
/Users/rs/Documents/exptini3 

(stackoverflow)[[email protected] Documents]# tree 
. 
|-- clients_file.txt 
|-- exptini1 
| |-- 01 
| | `-- middleware 
| |  `-- client-logs-archive.tar 
| |-- 02 
| | `-- middleware 
| |  `-- client-logs-archive.tar 
| |-- 03 
| | `-- middleware 
| |  `-- client-logs-archive.tar 
| |-- 04 
| | `-- middleware 
| |  `-- client-logs-archive.tar 
| |-- 05 
| | `-- middleware 
| |  `-- client-logs-archive.tar 
| |-- 06 
| | `-- middleware 
| |  `-- client-logs-archive.tar 
| |-- 07 
| | `-- middleware 
| |  `-- client-logs-archive.tar 
| |-- 08 
| | `-- middleware 
| |  `-- client-logs-archive.tar 
| `-- 09 
|  `-- middleware 
|   `-- client-logs-archive.tar 
|-- exptini2 
| |-- 01 
| | `-- middleware 
| |  `-- client-logs-archive.tar 
| |-- 02 
| | `-- middleware 
| |  `-- client-logs-archive.tar 
| |-- 03 
| | `-- middleware 
| |  `-- client-logs-archive.tar 
| |-- 04 
| | `-- middleware 
| |  `-- client-logs-archive.tar 
| |-- 05 
| | `-- middleware 
| |  `-- client-logs-archive.tar 
| |-- 06 
| | `-- middleware 
| |  `-- client-logs-archive.tar 
| |-- 07 
| | `-- middleware 
| |  `-- client-logs-archive.tar 
| |-- 08 
| | `-- middleware 
| |  `-- client-logs-archive.tar 
| `-- 09 
|  `-- middleware 
|   `-- client-logs-archive.tar 
`-- exptini3 
    |-- 01 
    | `-- middleware 
    |  `-- client-logs-archive.tar 
    |-- 02 
    | `-- middleware 
    |  `-- client-logs-archive.tar 
    |-- 03 
    | `-- middleware 
    |  `-- client-logs-archive.tar 
    |-- 04 
    | `-- middleware 
    |  `-- client-logs-archive.tar 
    |-- 05 
    | `-- middleware 
    |  `-- client-logs-archive.tar 
    |-- 06 
    | `-- middleware 
    |  `-- client-logs-archive.tar 
    |-- 07 
    | `-- middleware 
    |  `-- client-logs-archive.tar 
    |-- 08 
    | `-- middleware 
    |  `-- client-logs-archive.tar 
    `-- 09 
     `-- middleware 
      `-- client-logs-archive.tar 

執行untarscript.py後:

(stackoverflow)[[email protected] Documents]# tree 
. 
|-- clients_file.txt 
|-- exptini1 
| |-- 01 
| | `-- middleware 
| |  |-- client-logs-archive.tar 
| |  `-- client-logs_exptini1_01 
| |   |-- test1.txt 
| |   |-- test2.txt 
| |   |-- test3.txt 
| |   |-- test4.txt 
| |   |-- test5.txt 
| |   |-- test6.txt 
| |   |-- test7.txt 
| |   |-- test8.txt 
| |   `-- test9.txt 
| |-- 02 
| | `-- middleware 
| |  |-- client-logs-archive.tar 
| |  `-- client-logs_exptini1_02 
| |   |-- test1.txt 
| |   |-- test2.txt 
| |   |-- test3.txt 
| |   |-- test4.txt 
| |   |-- test5.txt 
| |   |-- test6.txt 
| |   |-- test7.txt 
| |   |-- test8.txt 
| |   `-- test9.txt 
| |-- 03 
| | `-- middleware 
| |  |-- client-logs-archive.tar 
| |  `-- client-logs_exptini1_03 
| |   |-- test1.txt 
| |   |-- test2.txt 
| |   |-- test3.txt 
| |   |-- test4.txt 
| |   |-- test5.txt 
| |   |-- test6.txt 
| |   |-- test7.txt 
| |   |-- test8.txt 
| |   `-- test9.txt 
| |-- 04 
| | `-- middleware 
| |  |-- client-logs-archive.tar 
| |  `-- client-logs_exptini1_04 
| |   |-- test1.txt 
| |   |-- test2.txt 
| |   |-- test3.txt 
| |   |-- test4.txt 
| |   |-- test5.txt 
| |   |-- test6.txt 
| |   |-- test7.txt 
| |   |-- test8.txt 
| |   `-- test9.txt 
| |-- 05 
| | `-- middleware 
| |  |-- client-logs-archive.tar 
| |  `-- client-logs_exptini1_05 
| |   |-- test1.txt 
| |   |-- test2.txt 
| |   |-- test3.txt 
| |   |-- test4.txt 
| |   |-- test5.txt 
| |   |-- test6.txt 
| |   |-- test7.txt 
| |   |-- test8.txt 
| |   `-- test9.txt 
| |-- 06 
| | `-- middleware 
| |  |-- client-logs-archive.tar 
| |  `-- client-logs_exptini1_06 
| |   |-- test1.txt 
| |   |-- test2.txt 
| |   |-- test3.txt 
| |   |-- test4.txt 
| |   |-- test5.txt 
| |   |-- test6.txt 
| |   |-- test7.txt 
| |   |-- test8.txt 
| |   `-- test9.txt 
| |-- 07 
| | `-- middleware 
| |  |-- client-logs-archive.tar 
| |  `-- client-logs_exptini1_07 
| |   |-- test1.txt 
| |   |-- test2.txt 
| |   |-- test3.txt 
| |   |-- test4.txt 
| |   |-- test5.txt 
| |   |-- test6.txt 
| |   |-- test7.txt 
| |   |-- test8.txt 
| |   `-- test9.txt 
| |-- 08 
| | `-- middleware 
| |  |-- client-logs-archive.tar 
| |  `-- client-logs_exptini1_08 
| |   |-- test1.txt 
| |   |-- test2.txt 
| |   |-- test3.txt 
| |   |-- test4.txt 
| |   |-- test5.txt 
| |   |-- test6.txt 
| |   |-- test7.txt 
| |   |-- test8.txt 
| |   `-- test9.txt 
| `-- 09 
|  `-- middleware 
|   |-- client-logs-archive.tar 
|   `-- client-logs_exptini1_09 
|    |-- test1.txt 
|    |-- test2.txt 
|    |-- test3.txt 
|    |-- test4.txt 
|    |-- test5.txt 
|    |-- test6.txt 
|    |-- test7.txt 
|    |-- test8.txt 
|    `-- test9.txt 
|-- exptini2 
| |-- 01 
| | `-- middleware 
| |  |-- client-logs-archive.tar 
| |  `-- client-logs_exptini2_01 
| |   |-- test1.txt 
| |   |-- test2.txt 
| |   |-- test3.txt 
| |   |-- test4.txt 
| |   |-- test5.txt 
| |   |-- test6.txt 
| |   |-- test7.txt 
| |   |-- test8.txt 
| |   `-- test9.txt 
| |-- 02 
| | `-- middleware 
| |  |-- client-logs-archive.tar 
| |  `-- client-logs_exptini2_02 
| |   |-- test1.txt 
| |   |-- test2.txt 
| |   |-- test3.txt 
| |   |-- test4.txt 
| |   |-- test5.txt 
| |   |-- test6.txt 
| |   |-- test7.txt 
| |   |-- test8.txt 
| |   `-- test9.txt 
| |-- 03 
| | `-- middleware 
| |  |-- client-logs-archive.tar 
| |  `-- client-logs_exptini2_03 
| |   |-- test1.txt 
| |   |-- test2.txt 
| |   |-- test3.txt 
| |   |-- test4.txt 
| |   |-- test5.txt 
| |   |-- test6.txt 
| |   |-- test7.txt 
| |   |-- test8.txt 
| |   `-- test9.txt 
| |-- 04 
| | `-- middleware 
| |  |-- client-logs-archive.tar 
| |  `-- client-logs_exptini2_04 
| |   |-- test1.txt 
| |   |-- test2.txt 
| |   |-- test3.txt 
| |   |-- test4.txt 
| |   |-- test5.txt 
| |   |-- test6.txt 
| |   |-- test7.txt 
| |   |-- test8.txt 
| |   `-- test9.txt 
| |-- 05 
| | `-- middleware 
| |  |-- client-logs-archive.tar 
| |  `-- client-logs_exptini2_05 
| |   |-- test1.txt 
| |   |-- test2.txt 
| |   |-- test3.txt 
| |   |-- test4.txt 
| |   |-- test5.txt 
| |   |-- test6.txt 
| |   |-- test7.txt 
| |   |-- test8.txt 
| |   `-- test9.txt 
| |-- 06 
| | `-- middleware 
| |  |-- client-logs-archive.tar 
| |  `-- client-logs_exptini2_06 
| |   |-- test1.txt 
| |   |-- test2.txt 
| |   |-- test3.txt 
| |   |-- test4.txt 
| |   |-- test5.txt 
| |   |-- test6.txt 
| |   |-- test7.txt 
| |   |-- test8.txt 
| |   `-- test9.txt 
| |-- 07 
| | `-- middleware 
| |  |-- client-logs-archive.tar 
| |  `-- client-logs_exptini2_07 
| |   |-- test1.txt 
| |   |-- test2.txt 
| |   |-- test3.txt 
| |   |-- test4.txt 
| |   |-- test5.txt 
| |   |-- test6.txt 
| |   |-- test7.txt 
| |   |-- test8.txt 
| |   `-- test9.txt 
| |-- 08 
| | `-- middleware 
| |  |-- client-logs-archive.tar 
| |  `-- client-logs_exptini2_08 
| |   |-- test1.txt 
| |   |-- test2.txt 
| |   |-- test3.txt 
| |   |-- test4.txt 
| |   |-- test5.txt 
| |   |-- test6.txt 
| |   |-- test7.txt 
| |   |-- test8.txt 
| |   `-- test9.txt 
| `-- 09 
|  `-- middleware 
|   |-- client-logs-archive.tar 
|   `-- client-logs_exptini2_09 
|    |-- test1.txt 
|    |-- test2.txt 
|    |-- test3.txt 
|    |-- test4.txt 
|    |-- test5.txt 
|    |-- test6.txt 
|    |-- test7.txt 
|    |-- test8.txt 
|    `-- test9.txt 
`-- exptini3 
    |-- 01 
    | `-- middleware 
    |  |-- client-logs-archive.tar 
    |  `-- client-logs_exptini3_01 
    |   |-- test1.txt 
    |   |-- test2.txt 
    |   |-- test3.txt 
    |   |-- test4.txt 
    |   |-- test5.txt 
    |   |-- test6.txt 
    |   |-- test7.txt 
    |   |-- test8.txt 
    |   `-- test9.txt 
    |-- 02 
    | `-- middleware 
    |  |-- client-logs-archive.tar 
    |  `-- client-logs_exptini3_02 
    |   |-- test1.txt 
    |   |-- test2.txt 
    |   |-- test3.txt 
    |   |-- test4.txt 
    |   |-- test5.txt 
    |   |-- test6.txt 
    |   |-- test7.txt 
    |   |-- test8.txt 
    |   `-- test9.txt 
    |-- 03 
    | `-- middleware 
    |  |-- client-logs-archive.tar 
    |  `-- client-logs_exptini3_03 
    |   |-- test1.txt 
    |   |-- test2.txt 
    |   |-- test3.txt 
    |   |-- test4.txt 
    |   |-- test5.txt 
    |   |-- test6.txt 
    |   |-- test7.txt 
    |   |-- test8.txt 
    |   `-- test9.txt 
    |-- 04 
    | `-- middleware 
    |  |-- client-logs-archive.tar 
    |  `-- client-logs_exptini3_04 
    |   |-- test1.txt 
    |   |-- test2.txt 
    |   |-- test3.txt 
    |   |-- test4.txt 
    |   |-- test5.txt 
    |   |-- test6.txt 
    |   |-- test7.txt 
    |   |-- test8.txt 
    |   `-- test9.txt 
    |-- 05 
    | `-- middleware 
    |  |-- client-logs-archive.tar 
    |  `-- client-logs_exptini3_05 
    |   |-- test1.txt 
    |   |-- test2.txt 
    |   |-- test3.txt 
    |   |-- test4.txt 
    |   |-- test5.txt 
    |   |-- test6.txt 
    |   |-- test7.txt 
    |   |-- test8.txt 
    |   `-- test9.txt 
    |-- 06 
    | `-- middleware 
    |  |-- client-logs-archive.tar 
    |  `-- client-logs_exptini3_06 
    |   |-- test1.txt 
    |   |-- test2.txt 
    |   |-- test3.txt 
    |   |-- test4.txt 
    |   |-- test5.txt 
    |   |-- test6.txt 
    |   |-- test7.txt 
    |   |-- test8.txt 
    |   `-- test9.txt 
    |-- 07 
    | `-- middleware 
    |  |-- client-logs-archive.tar 
    |  `-- client-logs_exptini3_07 
    |   |-- test1.txt 
    |   |-- test2.txt 
    |   |-- test3.txt 
    |   |-- test4.txt 
    |   |-- test5.txt 
    |   |-- test6.txt 
    |   |-- test7.txt 
    |   |-- test8.txt 
    |   `-- test9.txt 
    |-- 08 
    | `-- middleware 
    |  |-- client-logs-archive.tar 
    |  `-- client-logs_exptini3_08 
    |   |-- test1.txt 
    |   |-- test2.txt 
    |   |-- test3.txt 
    |   |-- test4.txt 
    |   |-- test5.txt 
    |   |-- test6.txt 
    |   |-- test7.txt 
    |   |-- test8.txt 
    |   `-- test9.txt 
    `-- 09 
     `-- middleware 
      |-- client-logs-archive.tar 
      `-- client-logs_exptini3_09 
       |-- test1.txt 
       |-- test2.txt 
       |-- test3.txt 
       |-- test4.txt 
       |-- test5.txt 
       |-- test6.txt 
       |-- test7.txt 
       |-- test8.txt 
       `-- test9.txt 

84 directories, 271 files 
+0

謝謝!它說p值= 「/%S/{0:0> 2} /中間件」 %(路徑[j]時,ⅰ) 類型錯誤:不串格式化 – LoveMeow

+0

過程中被轉化的所有參數,它仍然不執行任何操作,即使我保持所有你的變化除了第26行的變化:( – LoveMeow

+1

我修正並測試了整個腳本,它現在適用於我的新變更。 –

相關問題