0

I am writing a script currently to write 1hr-data into 4 files with the same name(i.e., timestamp). When I do not zip them File1: 1609306077 and File2: 1609309671. When the Zip function is not applied, the files are written individually and everything looks great.

    Data_zip = zipfile.ZipFile('location' + str(timestamp) + '.zip', 'w')  

    for folder, subfolders, files in os.walk('location'):
        for file in files:
            if file.startswith(str(timestamp)):
                Data_zip.write(os.path.join(folder, file), os.path.relpath(os.path.join(folder,file), 'location'), compress_type = zipfile.ZIP_DEFLATED) 
    Data_zip.close()

Once this code is being used. The files are like File1 contains the file2, file3 and so on. I realise the problem is one of the files is not closing. I'd also tried file.close() and it does not work. Is there any other efficient method to zip the files? or can I forcibly close the file using Python?

  • you are not saving archive in the same location your files are? – Serge Jan 04 '21 at 17:11
  • I am saving them in the same location – muth.code13 Jan 04 '21 at 17:53
  • to be sure that you do not compressing the archive into itself try `if file.startswith(timestamp) and not file.startswith('timestamp'):` – Serge Jan 04 '21 at 23:53
  • Do you have to do it in python? Why don't you `tar`? – aerijman Jan 05 '21 at 01:26
  • What are you trying to do? What's the problem? I don't see the code you posted **write** to any files, except the zip file. // Or perhaps you want to check if the file is opened by **another** process instead? – user202729 Jan 05 '21 at 01:32
  • If another process is creating the log files, perhaps it should write an additional semaphore file as well to inform your python program that files are ready to be zipped. – RufusVS Jan 05 '21 at 02:57

0 Answers0