我有一个文件夹结构为:如何只压缩文件夹内的文件而不压缩子文件夹?
/sample/debug/
--debug.exe
--sample.exe
--sample.pdb
--debug.pdb
--sample.dll
--debug.dll
/config
--sample.txt
--new.txt
/general
--general.txt
--code.txt
所以,我要的是只把拉链内部调试中的文件,而不是像子文件夹/配置和/一般。我试过如下:
import zipfile
import os
def append(dir_name):
ret_val = []
fileList = []
for file in os.listdir(dir_name):
try:
dirfile = os.path.join(dir_name, file)
except Exception:
err = sys.exc_info()
print ("Error!", err)
fileList.append(dirfile)
ret_val = fileList
return ret_val
def zip(fileList, archive, root):
ret_val = 0
try:
zip_folder_contents = zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED)
except Exception:
err = sys.exc_info()
print ("Error!",err)
exit(1)
for filename in fileList:
zip_folder_contents.write(filename,
filename[len(root):].lstrip(os.path.sep).lstrip(os.path.altsep))
zip_folder_contents.close()
return ret_val
make(append_files_in_zipfolder("D:/sample/debug"), "debug.zip",
"D:/sample/debug")
现在,当我执行上面我得到错误的权限被拒绝“d:/样品/调试\\配置”。因此,我无法删除此错误,所以我认为只包含zip文件夹内的文件并排除子文件夹。所以,无论如何,如何做到这一点,或者我可以删除我得到的这种权限相关的错误。请建议。
什么是'make()'?函数'append_files_in_zipfolder()'的定义在哪里? – martineau
查看''''os'''模块的[Files and Directories](https://docs.python.org/3/library/os.html#files-and-directories)部分,选择一些方法将帮助你清除目录,制定一些逻辑清除目录,把它们放在一起并压缩文件。 '''''''''''''可能会过度杀伤,但它可能会使它变得简单。还有其他一些可以使其变得简单/可能的其他方法。 – wwii