How to print the percentage of python files

I would like to get the percentage in which the file is located at the time it zips it up. For example, it will print 1%, 2%, 3%, etc. I have no idea where to start. As I will do this right now, I have the code for the zip file.

code:

zipPath = zipfile.ZipFile("Files/Zip/" + pic + ".zip", "w") for root, dirs, files in os.walk(filePath): for file in files: zipPath.write(os.path.join(root, file), str(pic) + "\\" + file) print("Done") zipPath.close() 
+6
source share
2 answers

Unfortunately, you cannot make progress in compressing each individual file from the zipfile module, but you can get an idea of ​​the overall progress by keeping track of how many bytes you have processed so far.

As Mikko Ortamaa suggested, the easiest way to do this is to double-view the list of files, first determine the file sizes, and then perform the compression. However, as Kevin said, the contents of the directory may change between these two passes, so the numbers may not be accurate.

The program below (written for Python 2.6) illustrates the process.

 #!/usr/bin/env python ''' zip all the files in dirname into archive zipname Use only the last path component in dirname as the archive directory name for all files Written by PM 2Ring 2015.02.15 From http://stackoverflow.com/q/28522669/4014959 ''' import sys import os import zipfile def zipdir(zipname, dirname): #Get total data size in bytes so we can report on progress total = 0 for root, dirs, files in os.walk(dirname): for fname in files: path = os.path.join(root, fname) total += os.path.getsize(path) #Get the archive directory name basename = os.path.basename(dirname) z = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED) #Current data byte count current = 0 for root, dirs, files in os.walk(dirname): for fname in files: path = os.path.join(root, fname) arcname = os.path.join(basename, fname) percent = 100 * current / total print '%3d%% %s' % (percent, path) z.write(path, arcname) current += os.path.getsize(path) z.close() def main(): if len(sys.argv) < 3: print 'Usage: %s zipname dirname' % sys.argv[0] exit(1) zipname = sys.argv[1] dirname = sys.argv[2] zipdir(zipname, dirname) if __name__ == '__main__': main() 

Note that I am opening a zip file with the argument zipfile.ZIP_DEFLATED compression; zipfile.ZIP_STORED used by zipfile.ZIP_STORED , that is, compression is not performed. In addition, zip files can work with both DOS-style path separators and Unix-style ones, so you don't need to use backslashes in your archive paths, and as my code shows, you can just use os.path.join() to create the path to the archive file.


By the way, in your code you have str(pic) inside the inner for loop. In general, this is a slightly wasteful reevaluation of a function with a constant argument inside a loop. But in this case, this is completely superfluous, since from your first statement it seems that pic already a string.

+2
source

The existing answer only works at the file level, i.e. if you have one huge file for zip, you will not see any progress until the completion of the whole operation. In my case, I only had one huge file, and I did something like this:

 import os import types import zipfile from functools import partial if __name__ == '__main__': out_file = "out.bz2" in_file = "/path/to/file/to/zip" def progress(total_size, original_write, self, buf): progress.bytes += len(buf) progress.obytes += 1024 * 8 # Hardcoded in zipfile.write print("{} bytes written".format(progress.bytes)) print("{} original bytes handled".format(progress.obytes)) print("{} % done".format(int(100 * progress.obytes / total_size))) return original_write(buf) progress.bytes = 0 progress.obytes = 0 with zipfile.ZipFile(out_file, 'w', compression=zipfile.ZIP_DEFLATED) as _zip: # Replace original write() with a wrapper to track progress _zip.fp.write = types.MethodType(partial(progress, os.path.getsize(in_file), _zip.fp.write), _zip.fp) _zip.write(in_file) 

Not optimal, since the number of hard-coded bytes can be changed for each write () call.

The function is also called quite often; updating the user interface should probably not be performed for every call.

-1
source

Source: https://habr.com/ru/post/982509/


All Articles