试图在/ tmp目录中生成和归档文件,但没有成功。 尝试了shututil和ZipFile。如果archive = True,则需要存档生成的文件。无法为Zip函数提供生成文件的路径。
def generate_test_file(count, emptylines=False, archive=True):
with tempfile.NamedTemporaryFile(mode='w', suffix='.LOG', delete=False) as file:
line_mock = "T\n\n" if emptylines else "T\n"
for i in range(count):
file.write(line_mock)
path_file = file.name
print(path_file)
if not archive:
return file.name
else:
# shutil.make_archive("test", "zip", path_file)
with zipfile.ZipFile('test.zip', 'w') as myzip:
myzip.write(path_file)
print(myzip.name)
return myzip.filename
generate_test_file(100)