Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我们正在使用 python 的 tarFile 库将给定文件集解压到目标目录。
tarGzipFile = tarfile.open(fileName) tarGzipFile.extractall(targetDir + '/')
在这里,如果同一文件存在于多个 tar.gz 文件中,则该文件将覆盖在目标目录中。如何更改我的代码以避免覆盖?并寻找类似于 tar --backup none 或 numbered 的选项...这样如果文件存在,则它以 ~ 为前缀或在末尾添加数字。
您可以使用 tarGzipFile.getmembers() 列出存档中的文件,并传递 members= 以仅提取那些您希望提取的文件(即排除已经存在的文件)。os.path.exists() 可用于检查文件是否存在。