[英]How to unzip files in the same subdirectories but a different folder
In my_directory
I have 3 folders (NY, AMS, MAD).在
my_directory
我有 3 个文件夹(NY、AMS、MAD)。 Each folder has 1 or more zipped files.每个文件夹都有 1 个或多个压缩文件。 I also have an other directory named
my_counterpart
.我还有一个名为
my_counterpart
目录。 This one is empty.这个是空的。
With below code Im trying to:使用下面的代码我试图:
my_counterpart
+ unzipp the files they posses`.my_counterpart
+ 解压缩它们拥有的文件。 This is my code:这是我的代码:
pattern = '*.zip'
for root, dirs, files in os.walk(my_directory):
for filename in fnmatch.filter(files, pattern):
path = os.path.join(root, filename)
new = os.path.join(my_counterpart, dirs)
zipfile.ZipFile(path).extractall(new)
I know where the fault lies, dirs
is not aa string but a list.我知道问题出在哪里,
dirs
不是字符串而是列表。 However I can't seem to solve it.但是我似乎无法解决它。 Someone here who can guide me?
这里有人可以指导我吗?
TypeError: join() argument must be str or bytes, not 'list'
Does the variable my_counterpart
countain the path to new folder?变量
my_counterpart
是否计入新文件夹的路径? If yes then why would you add something else to it like dirs
?如果是,那么您为什么要向其添加其他内容,例如
dirs
? Leaving it out already does what you want it to do.离开它已经可以做你想让它做的事情。 What's left to do is to create the folder structure and extract into that newly created folder structure:
剩下要做的是创建文件夹结构并提取到新创建的文件夹结构中:
pattern = '*.zip'
for root, dirs, files in os.walk(my_directory):
for filename in fnmatch.filter(files, pattern):
path = os.path.join(root, filename)
# Store the new directory so that it can be recreated
new_dir = os.path.normpath(os.path.join(os.path.relpath(path, start=my_directory), ".."))
# Join your target directory with newly created directory
new = os.path.join(my_counterpart, new_dir)
# Create those folders, works even with nested folders
if (not os.path.exists(new)):
os.makedirs(new)
zipfile.ZipFile(path).extractall(new)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.