【发布时间】:2017-11-08 10:46:28
【问题描述】:
我正在尝试从存储在文本文件中的文件列表创建 tar 文件,我有创建 tar 的工作代码,但我希望从某个目录(应用程序和所有子目录)启动存档,并且删除父目录。这是因为软件只从某个目录打开文件。
package.list 文件如下:
app\myFile
app\myDir\myFile
app\myDir\myFile2
如果我省略了restore.add 中的路径,由于我的程序从其他地方运行,它无法找到文件。如何告诉 tar 从特定目录开始,或添加文件,但保持从文本文件中获得的目录结构,例如以 app 开头,而不是所有父目录
我的目标是 tar cf restore.tar -T package.list 但在 Windows 上使用 Python。
我从这里尝试过basename:How to compress a tar file in a tar.gz without directory?,这会删除所有目录。
我也尝试在 .add 方法中使用 arcname='app',但这会破坏目录结构并将文件负载重命名为 app,从而产生一些奇怪的结果
path = foo + '\\' + bar
file = open(path + '\\package.list', 'r')
restore = tarfile.open(path + '\\restore.tar', 'w')
for line in file:
restore.add(path + '\\' + line.strip())
restore.close()
file.close()
使用 Python 2.7
【问题讨论】:
标签: python python-2.7 tar