【发布时间】:2021-05-01 06:12:49
【问题描述】:
我正在尝试压缩给定目录,然后立即删除该目录。
因此我使用shutil 模块。
我实现的方法如下所示:
def _zip(fulldname, delete_after=False):
"""
Zip a directory
:param dname: The full name of the directory
:type dname: str
:param delete_after: Delete the directory after it has been zipped?
:type delete_after: bool
:return: the full name of the zip file
:rtype: str
"""
if not os.path.isdir(fulldname):
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT),
fulldname)
fullzipfname = shutil.make_archive(fulldname, "zip", root_dir=fulldname,
base_dir="./")
if delete_after:
shutil.rmtree(fulldname)
return fullzipfname
当我执行它时,我要么收到以下错误,要么该方法永远运行(即卡在 shutil.rmtree() 方法上):
File "/media/sf_projects/robot_framework/rfw_rest_api/src/main/rfw_rest_api/rfw_rest_api.py", line 126, in exec_robot
output_arc = core._zip(proc_output_dir, delete_after=True)
File "/media/sf_projects/robot_framework/rfw_rest_api/src/main/core.py", line 147, in _zip
shutil.rmtree(fulldname)
File "/usr/lib/python3.7/shutil.py", line 498, in rmtree
onerror(os.rmdir, path, sys.exc_info())
File "/usr/lib/python3.7/shutil.py", line 496, in rmtree
os.rmdir(path)
OSError: [Errno 26] Text file busy: '../../output/pcap_based_fuzzer_210127_120207'
我做错了什么?
【问题讨论】: