Python Forum
Progress Finished Question - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: General Coding Help (https://python-forum.io/Forum-General-Coding-Help)
+--- Thread: Progress Finished Question (/Thread-Progress-Finished-Question)

Pages: 1 2 3 4


Progress Finished Question - malonn - May-07-2018

Hi all. First post, very new to Python and programming in general. K, now the question:
I have this script:
import sys, shutil, subprocess, ffmpy

def getDimensions(inputVideo):
    args = '-v error -select_streams v:0 -show_entries stream=width,height ' \
           '-of compact'
    call = ffmpy.FFprobe(r'E:\\App Back-Ups\FFprobe\ffprobe.exe', args, \
                         {inputVideo : None})
    return call.run(None, subprocess.PIPE)

def getHeight(tupleIn):
    a = str(tupleIn[0])
    b = a.rfind('=')
    return a[b + 1:b + 4]

toMove = sys.argv[1]
output = getDimensions(toMove)
height = getHeight(output)
if height == '480':
    pathStr1 = 'E:\\My Movies\DVD Rips\\'
    pathStr2 = 'G:\\My Movies\DVD Rips'
else:
    pathStr1 = 'E:\\My Movies\Blu-ray Rips\\'
    pathStr2 = 'G:\\My Movies\Blu-ray Rips'

lastSep = toMove.rfind('\\')
toCopy = pathStr1 + toMove[lastSep + 1:]
shutil.move(toMove, pathStr1)
shutil.copy(toCopy, pathStr2)
input('Press \'Enter\' to exit...')
sys.exit()
It works fine, but sometimes the movie file sizes are quite large and they take a while to copy. I'm wondering how to add a progress bar or percent (preferable) to the runtime. I'm stuck figuring out how because shutil pauses while it's processing the move/copy and does not pick up the next line of script until after it has finished the copy. So, is it possible to add a simple percentage copied output to this script? Just so I know how much is done and how much more is left.


RE: Progress Finished Question - Larz60+ - May-08-2018

Think about what you're asking. How can you know % done if you have no communication from the program that is using the time?


RE: Progress Finished Question - malonn - May-08-2018

I guess you can't? I know that shutil has no function that returns progress complete. I was thinking I could compare the size of the file with how much has been copied/moved as it happens. But, the script pauses at the copy/move functions until the file operation is done.


RE: Progress Finished Question - wavic - May-08-2018

Well, you can run shutil.copy/move in a separate thread and compare the file size against the destination file and do some math to get the percentage or some other measure. You can use this and pass it to the tqdm module for example.


RE: Progress Finished Question - malonn - May-08-2018

Sweet! Thanks, @wavic. I'm about as noob as they come so know nothing about threads, but Google and the official docs do. I'll look into this further.


RE: Progress Finished Question - wavic - May-08-2018

Consider that it will takes some time to check the size of copied file and compare it during to copying process. So the value will have a small difference.


RE: Progress Finished Question - malonn - May-09-2018

So, it won't be terribly accurate? Average file size is about 6GB.


RE: Progress Finished Question - wavic - May-09-2018

It will be fine.


RE: Progress Finished Question - malonn - May-09-2018

Great. I must confess, threading makes my brain hurt. It's a lot for a greenhorn noob. Oh well. Thanks again, @wavic.


RE: Progress Finished Question - wavic - May-09-2018

I am not very familiar with threading because I didn't use it.
The simplest approach:

import thread

source = '/path/big_file.BIG'
dest_path = '/path'
thread.start_new_thread(shutil.copy, (source, dest_path))
You provide the callable and the arguments to the start_new_thread method and that's all.
But this won't tell you if the copping is finished.

Better see this. It's basically almost the same. On top