如何在 python 中使用 linux 命令遍历 .tar.gz 文件列表
How to loop through the list of .tar.gz files using linux command in python
使用 python 2.7
我在 linux 框中有一个 *.tat.gz 文件列表。使用 python,我想遍历文件并将这些文件提取到不同位置,在它们各自的文件夹下。
For example: if my file name is ~/TargetData/zip/1440198002317590001.tar.gz
then I want to untar and ungzip this file in a different location under its
respective folder name i.e. ~/TargetData/unzip/1440198002317590001.
我已经写了一些代码,但我无法遍历这些文件。在命令行中,我可以使用 $ tar -czf 1440198002317590001.tar.gz 1440198002317590001 命令取消tar。但我希望能够遍历 .tar.gz 文件。代码在下面提到。在这里,我不能只循环文件或只打印文件。你能帮忙吗?
import os
inF = []
inF = str(os.system('ls ~/TargetData/zip/*.tar.gz'))
#print(inF)
if inF is not None:
for files in inF[:-1]:
print files
"""
os.system('tar -czf files /unzip/files[:-7]')
# This is what i am expecting here files = "1440198002317590001.tar.gz" and files[:-7]= "1440198002317590001"
"""
您曾经处理过此类用例吗?非常感谢您的帮助!!谢谢!
我认为你误解了 os.system()
的意思,它会完成工作,但它的 return 值不是你所期望的,它 returns 0 表示成功完成,您不能直接将其输出分配给变量。您可以考虑模块 [subprocess]
,请参阅文档 here。但是,我不推荐这种方式来列出文件(实际上,它是 returns 字符串而不是列表,请参阅文档自行查找详细信息)。
我认为最好的方法是 glob
模块,请参阅文档 here。使用glob.glob(pattern)
,你可以将所有匹配模式的文件放在一个列表中,然后你可以很容易地循环它。
当然,如果您熟悉os
模块,您也可以使用os.listdir()
、os.path.join()
,甚至os.paht.expanduser()
来实现。 (与glob
不同的是,它只是将没有完整路径的文件名放入列表中,您需要重建文件路径)。
顺便说一句,为了您的目的,无需先声明一个空列表(即 inF = []
)
对于解压文件部分,你可以通过os.system
来完成,但我也建议使用subprocess
模块而不是os.system
,你会在文档中找到原因subprocess
.
不要看下面的代码,只有当你实在无法自己解决这个问题时才看。
import os
import glob
inF = glob.glob('~/TargetData/zip/*.tar.gz')
if inF:
for files in inF:
# consider subprocess.call() instead of os.system
unzip_name = files.replace('zip', 'unzip')[:-7]
# get directory name and make sure it exists, otherwise create it
unzip_dir = os.path.dirname(unzip_name)
if not os.path.exists(unzip_dir):
os.mkdir(unzip_dir)
subprocess.call(['tar -xzf', files, '-C', unzip_name])
# os.system('tar -czf files /unzip/files[:-7]')
使用 python 2.7
我在 linux 框中有一个 *.tat.gz 文件列表。使用 python,我想遍历文件并将这些文件提取到不同位置,在它们各自的文件夹下。
For example: if my file name is ~/TargetData/zip/1440198002317590001.tar.gz
then I want to untar and ungzip this file in a different location under its respective folder name i.e. ~/TargetData/unzip/1440198002317590001.
我已经写了一些代码,但我无法遍历这些文件。在命令行中,我可以使用 $ tar -czf 1440198002317590001.tar.gz 1440198002317590001 命令取消tar。但我希望能够遍历 .tar.gz 文件。代码在下面提到。在这里,我不能只循环文件或只打印文件。你能帮忙吗?
import os
inF = []
inF = str(os.system('ls ~/TargetData/zip/*.tar.gz'))
#print(inF)
if inF is not None:
for files in inF[:-1]:
print files
"""
os.system('tar -czf files /unzip/files[:-7]')
# This is what i am expecting here files = "1440198002317590001.tar.gz" and files[:-7]= "1440198002317590001"
"""
您曾经处理过此类用例吗?非常感谢您的帮助!!谢谢!
我认为你误解了 os.system()
的意思,它会完成工作,但它的 return 值不是你所期望的,它 returns 0 表示成功完成,您不能直接将其输出分配给变量。您可以考虑模块 [subprocess]
,请参阅文档 here。但是,我不推荐这种方式来列出文件(实际上,它是 returns 字符串而不是列表,请参阅文档自行查找详细信息)。
我认为最好的方法是 glob
模块,请参阅文档 here。使用glob.glob(pattern)
,你可以将所有匹配模式的文件放在一个列表中,然后你可以很容易地循环它。
当然,如果您熟悉os
模块,您也可以使用os.listdir()
、os.path.join()
,甚至os.paht.expanduser()
来实现。 (与glob
不同的是,它只是将没有完整路径的文件名放入列表中,您需要重建文件路径)。
顺便说一句,为了您的目的,无需先声明一个空列表(即 inF = []
)
对于解压文件部分,你可以通过os.system
来完成,但我也建议使用subprocess
模块而不是os.system
,你会在文档中找到原因subprocess
.
不要看下面的代码,只有当你实在无法自己解决这个问题时才看。
import os
import glob
inF = glob.glob('~/TargetData/zip/*.tar.gz')
if inF:
for files in inF:
# consider subprocess.call() instead of os.system
unzip_name = files.replace('zip', 'unzip')[:-7]
# get directory name and make sure it exists, otherwise create it
unzip_dir = os.path.dirname(unzip_name)
if not os.path.exists(unzip_dir):
os.mkdir(unzip_dir)
subprocess.call(['tar -xzf', files, '-C', unzip_name])
# os.system('tar -czf files /unzip/files[:-7]')