【问题标题】:Untar file using python no error/warnings?使用python解压文件没有错误/警告?
【发布时间】:2015-10-27 00:47:43
【问题描述】:

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

所以我的output_text 文件有我的主目录,然后我循环遍历该目录中的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 行,打开 raise ReadError("文件无法成功打开") tarfile.ReadError: 文件无法成功打开

这是我修改后的代码:

导入操作系统 导入 os.path 导入tar文件

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 文件!!

【问题讨论】:

    标签: python tar


    【解决方案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&gt;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 的测试失败。

    tar.extractall() 添加了path=p 参数,以确保将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
    

    【讨论】:

    • 谢谢!它说 p = "/%s/{0:0>2}/middleware"%(path[j],i) TypeError: not all arguments convert during string formatting
    • 它仍然什么都不做,即使我保留了除第 26 行之外的所有更改:(
    • 我修复并测试了整个脚本。它现在适用于我的新更改。
    猜你喜欢
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 2017-08-08
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    相关资源
    最近更新 更多