0

I'm using the subprocess module to write large files to a usb flash drive like so:

import subprocess
subprocess.Popen("diskutil unMount /dev/diskX", shell=True)
subprocess.Popen("sudo dd if='file_name' of='/dev/diskX'", shell=True)

Ultimately, I just want to add in a line that fetches the % complete every few seconds so I can see how quickly it is going for very large transfers

I know when I run this command in terminal that I can type "SIGINFO" and press "Ctrl+T" while it is transferring to get the amount transferred but I don't know to convert that into a Python script.

I'm making the transfer after unmounting the disk so I can't just query the drives new files as they transfer and compare that as a % against the original file size.

Matt
  • 1,368
  • 1
  • 26
  • 54
  • doing that without `dd` (in pure python) would make your task easier. Check `shutil.copyfileobj` for instance (you'd have to run your python script with `sudo` though) – Jean-François Fabre Jun 16 '18 at 20:36
  • @Jean-Francois Fabre - sorry I just clarified my question. I have used `shutil.copyfileobj` and that works just fine but it doesn't really solve my overarching question of how to spit back out the transfer % in real time. – Matt Jun 16 '18 at 20:57
  • mix `write` with a buffer size that you control + a progress bar, in a loop. – Jean-François Fabre Jun 16 '18 at 21:05
  • for instance: https://stackoverflow.com/questions/274493/how-to-copy-a-file-in-python-with-a-progress-bar – Jean-François Fabre Jun 16 '18 at 21:06
  • or https://stackoverflow.com/questions/29967487/get-progress-back-from-shutil-file-copy-thread – Jean-François Fabre Jun 16 '18 at 21:07
  • You relinquish control when you pass the buck to `dd`. But you can pass the buck to some other tool which provides a progress bar. Does your `dd` support `status=progress`? Or look at `pv`. – tripleee Jun 16 '18 at 21:15
  • As an aside, you really should get rid of the `shell=True`. Python seems pretty redundant here if all you do is call the shell anyway. See also https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess – tripleee Jun 16 '18 at 21:20
  • @tripleee `shell=True` is required when argument is a string and not a `list` (which makes _two_ bad choices) – Jean-François Fabre Jun 16 '18 at 21:49
  • Hey `pv` worked - thank you @tripleee. This isn't the whole script fellas, it's just the relevant section for brevity. What's wrong with using a a string with `shell=True`? – Matt Jun 16 '18 at 22:46
  • @JeanFrançoisFabre Indeed, part of the fix is parsing the oommand into a list yourself instead of having the shell do it. There are helper functions which can do this #or you, though in this case it's obviously trivial to do it yourself. – tripleee Jun 17 '18 at 06:07
  • @Matt See the "see also" link I provided. That's why I provided it. – tripleee Jun 17 '18 at 06:08

0 Answers0