我有一个ROOT_FOLDER_PATH与子文件夹A,B,C,D,E(...),我想压缩文件夹A,C和D到一个压缩文件EXCLUDING所有其他和ROOT_FOLDER_PATH本身与Python 2.7。我怎么做?谢谢你的帮助。Python - 如何将几个文件夹从一个路径压缩到一个zip文件?
-2
A
回答
0
这比原来的问题更进一步。 提供从根文件夹中选择要压缩的目录和用于排除所选每个目录内的子目录和文件的选项的选项。
import os
import zipfile
def zipSelectExclude(src, dst, incluDIR1, incluDIR2, incluDIR3, excluSUBDIR1a, excluSUBDIR1b, excluSUBDIR3a, excluFILE3):
zf = zipfile.ZipFile("%s.zip" % (dst), "w", zipfile.ZIP_DEFLATED)
abs_src = os.path.abspath(src)
for dirname, subdirs, files in os.walk(src):
for filename in files:
absname = os.path.abspath(os.path.join(dirname, filename))
arcname = absname[len(abs_src) + 1:]
print 'zipping %s as %s' % (os.path.join(dirname, filename),
arcname)
if incluDIR1 in dirname and excluSUBDIR1a not in dirname and excluSUBDIR1b not in dirname or incluDIR2 in dirname or incluDIR3 in dirname and excluSUBDIR3a not in dirname and excluFILE3 not in filename:
zf.write(absname, arcname),
zf.close()
""" Got the above from https://stackoverflow.com/questions/27991745/python-zip-file-and-avoid-directory-structure (answer from user 12321)
and added the 'if' statement (and the 'def' modifications) to include selected folders from root folder to zip and exclude subdirs and files.
Be careful with the 'if' statement hierarchical order - for each folder inclusion 'in dirname' specify it's subdirectories and file exclusion
with 'and ... not in dirname' nx"""
def zipSE():
src = os.path.join('/PATH/TO/MY', 'rootFOLDER')
dst = os.path.join('/PATH/TO/SAVE/MY', 'ZIPfile')
incluDIR1 = os.path.join(src, 'incluDIR1')
incluDIR2 = os.path.join(src, 'incluDIR2')
incluDIR3 = os.path.join(src, 'incluDIR3')
excluSUBDIR1a = os.path.join(incluDIR1, 'excluSUBDIR1a')
excluSUBDIR1b = os.path.join(incluDIR1, 'excluSUBDIR1b')
excluSUBDIR3a = os.path.join(incluDIR3, 'excluSUBDIR3a')
zipSelectExclude(src, dst, incluDIR1, incluDIR2, incluDIR3, excluSUBDIR1a, excluSUBDIR1b, excluSUBDIR3a, 'excluFILE3')
zipSE()
0
这是直接的答案,原来的问题:
import os
import zipfile
def zipONLY(src, dst, incluDIR1, incluDIR2, incluDIR3):
zf = zipfile.ZipFile("%s.zip" % (dst), "w", zipfile.ZIP_DEFLATED)
abs_src = os.path.abspath(src)
for dirname, subdirs, files in os.walk(src):
for filename in files:
absname = os.path.abspath(os.path.join(dirname, filename))
arcname = absname[len(abs_src) + 1:]
print 'zipping %s as %s' % (os.path.join(dirname, filename),
arcname)
""" Change the arguments and the 'if' statement to fit your needs"""
if incluDIR1 in dirname or incluDIR2 in dirname or incluDIR3 in dirname:
zf.write(absname, arcname),
zf.close()
0
这是oposite:压缩所有除...
import os
import zipfile
def zipALLexcept(src, dst, excluDIR1, excluDIR2, excluDIR3):
zf = zipfile.ZipFile("%s.zip" % (dst), "w", zipfile.ZIP_DEFLATED)
abs_src = os.path.abspath(src)
for dirname, subdirs, files in os.walk(src):
for filename in files:
absname = os.path.abspath(os.path.join(dirname, filename))
arcname = absname[len(abs_src) + 1:]
print 'zipping %s as %s' % (os.path.join(dirname, filename),
arcname)
""" Change the arguments and the 'if' statement to fit your needs."""
if excluDIR1 not in dirname and excluDIR2 not in dirname and excluDIR3 not in dirname:
zf.write(absname, arcname),
zf.close()
相关问题
- 1. 如何将一个zip文件移动到一个文件夹
- 2. 使用Python将文件解压缩到一个文件夹
- 3. 如何创建一个批处理文件,将压缩一个.zip文件中的几个不同的文件
- 4. 如何从一个文件夹路径
- 5. 将多个文件压缩到一个文件夹
- 6. 如何从一个.zip解压特定文件夹与Python
- 7. 如何将文件夹压缩为zip?
- 8. 如何将多个文件夹压缩到一个存档中?
- 9. 如何将文件从zip压缩文件传输到另一个压缩文件而不需要进行decrompressing
- 10. 如何从一个压缩文件
- 11. 如何使用PowerShell将多个文件压缩为一个zip文件?
- 12. 如何使用VBScript将文件夹的每个子文件夹压缩为ZIP压缩文件?
- 13. 如何通过logback将multy文件压缩到一个zip文件?
- 14. 将几个文件夹移动到一个文件夹中
- 15. 将zip文件压缩到一起
- 16. 你如何上传一个zip压缩文件到OpenShift应用
- 17. 如何将文件从一个文件夹移动到另一个文件夹?
- 18. 如何将文件从一个文件夹移动到另一个文件夹?
- 19. 如何将文件从一个文件夹复制到另一个文件夹
- 20. 如何创建一个循环,将压缩文件夹,然后压缩文件夹“x”的时间量,Python 3.4?
- 21. Python - 我试图解压缩一个文件,其中有多个zip文件
- 22. 将node.js中的文件解压缩为一个url路径
- 23. 如何N个文件压缩到N .zip文件分别
- 24. 创建一个ZIP压缩文件而不压缩?
- 25. 如何设置一个压缩文件来压缩只有一个CSS文件
- 26. 如何用动态路径或多个路径压缩文件?
- 27. 如何批量压缩在一个.zip文件的多个文件
- 28. Java ZIP - 如何解压缩文件夹?
- 29. python/zip:如果提供绝对路径文件,如何消除zip压缩文件中的绝对路径?
- 30. 使用ZipFile类从多个文件的zip压缩文件解压缩文件
欢迎SO。本网站不是代码编写服务,不适用于提供完整的解决方案。预计用户将展示一些努力和代码,而SO在此期间将帮助您解决具体的编程问题。你有没有尝试过任何东西? –
对不起...我是新来的SO或编程,大约三个星期前我开始自学python,但我不知道这个(很棒!)网站道德。但是我发现了一个基于另一个代码的解决方案来完成我想要的功能。首先,我得到了一些能够拉伸所有东西的东西(之前删除了我不想要的东西)。然后,我开始用逻辑和“如果”,“和”,“或”,“不是”,“在”中“玩”,我已经达到了这个目标。我希望不要放弃。谢谢。 –