如何确定python ZipFile中的项是dir还是file?
这是我当前的系统,它只是查看路径中是否有一个周期。。。但这并不适用于所有情况如何确定python ZipFile中的项是dir还是file?,python,python-3.x,file,zip,zipfile,Python,Python 3.x,File,Zip,Zipfile,这是我当前的系统,它只是查看路径中是否有一个周期。。。但这并不适用于所有情况 with ZipFile(zipdata, 'r') as zipf: #Open the .zip BytesIO in Memory mod_items = zipf.namelist() #Get the name of literally everything in the zip for folder in mod_folde
with ZipFile(zipdata, 'r') as zipf: #Open the .zip BytesIO in Memory
mod_items = zipf.namelist() #Get the name of literally everything in the zip
for folder in mod_folders:
mod_items.insert(0, folder)
#Put the folder at the start of the list
#This makes sure that the folder gets created
#Search through every file in the .zip for each folder indicated in the save
for item in mod_items:
if folder in item: #If the item's path indicates that it is a member of the folder
itempath = item #Make a copy of the
if not itempath.startswith(folder):
itempath = path_clear(itempath, folder)
if not 'GameData/' in itempath:
itempath = 'GameData/' + itempath
path = mod_destination_folder + '/' + itempath
path = path_clear(path)
dot_count = path.count('.')
if dot_count: #Is a normal file, theoretically
itemdata = zipf.read(item)
try:
with open(path, 'wb') as file:
file.write(itemdata)
except FileNotFoundError as f:
folder_path = path[:path.rfind('/')]
makedirs(folder_path)
with open(path, 'wb') as file:
file.write(itemdata)
else: #Is a folder
try: makedirs(path)
except: pass
我需要的只是某种方式来确定:
A) 文件夹是否为用户所需的文件夹之一
B) 对于该文件夹中的每个项目都是目录或文件,请使用:
您还可以使用infolist
而不是namelist
直接获取ZipInfo对象,而不是名称
或者,检查名称-目录将以
/
结尾。(这与is_dir
执行的检查相同。)So用于zipf.namelist()中的名称:如果zipf.getinfo(name).is_dir()
?@artificialnintelligence:或使用infolist
。我可能会选择infolist
。然后我该如何处理每个ZipInfo对象?另外,我正在努力降低内存使用率,因为一台机器上会同时运行多达16个这样的实例,所以它们是大的吗?
zipf.getinfo(name).is_dir()