使用python的Untar文件没有错误/警告?

时间:2015-10-27 00:47:44

标签: python tar

我正在尝试解压 python 中的文件。我给我的程序一个文件路径,文件的主目录有9个子目录,python循环遍历它们,查找具有特定名称的tar文件并解压缩它们。

所以我的output_text文件有我的主目录,然后我循环遍历该目录中的01-09文件号和解压缩文件,其中包含某些名称。< / p>

我已经编写了一些代码但发生了什么没有,没有错误,没有任何警告

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 

&#34; /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tarfile.py" ;,第1672行,公开         引发ReadError(&#34;文件无法成功打开&#34;)     tarfile.ReadError:无法成功打开文件

这是我的修改后的代码:

导入操作系统 import 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 个答案:

答案 0 :(得分: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之前:

[root@joeyoung.io Documents]# pwd
/Users/rs/Documents
[root@joeyoung.io 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
[root@joeyoung.io Documents]# cat clients_file.txt
/Users/rs/Documents/exptini1
/Users/rs/Documents/exptini2
/Users/rs/Documents/exptini3

(stackoverflow)[root@joeyoung.io 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)[root@joeyoung.io 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