Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Progress Finished Question
#21
Okay, it's a progress bar (sorry, I hadn't noticed the link); I'll look into it. I kind of want to write it out on my own just for the practice. Thanks for taking the time to write out an explanation. I need to know the errors to make use of it I guess?

Well, let m e see what I can come up with (or get to a point that I need help) and I'll post back with some code.
Reply
#22
You don't need to know all the error or any at all. Just test the code.
In my example, there is an input which is converted to float. Open the Python interpreter and run this line of code but instead of some number put a letter or some punctuation as an input. Produce an error on purpose. And you will see how is called.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#23
Okay, here is version 1.1:

import os, sys

def copyFunc(source, destination):
    with open(source, 'rb') as f1, open(destination, 'wb') as f2:
        fullSize = os.stat(source).st_size
        increment = 10485760
        while os.stat(destination).st_size < fullSize:
            chunk = f1.read(increment)
            f2.write(chunk)
            full = os.stat(destination).st_size
            print(round(full / fullSize * 100, 1), '%\r')

one = '397.64-desktop.exe'
two = 'C:\\Users\Mark\Downloads\Ordenador\driver.exe'
copyFunc(one, two)
I put it in a function and increased byte size to what you said, and with that, it's closer to SSD speed. I got the percentage to work better, but I'm not 100% happy. It's a WIP, for sure.
Reply
#24
The while condition is unnecessary.
Instead of calling a function on each loop you can initialise the chunk before the loop. And you will have one function call less in the script.

increment = 10485760
chunk = f1.read(increment)
        while chunk:
            f2.write(chunk)
            full = os.stat(destination).st_size
            print(round(full / fullSize * 100, 1), '%\r')
            chunk = f1.read(increment)
Alson, you can play with the 'increment' size to see what is the optimum one for your system.
Why are you not happy?

Btw, try to calculate it to see if there is any difference.

increment = 10485760
copied = 0
chunk = f1.read(increment)
        while chunk:
            f2.write(chunk)
            copied += increment
            print(round(copied / fullSize * 100, 1), '%\r')
            chunk = f1.read(increment)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#25
Alrighty, there wavic. You're sticking with me and I appreciate it. I'll try some things and see what's what. I'm thinking "increment" is going to vary based on file size--a smaller file will reduce "increment", a larger will do the opposite. I don't know yet. Maybe I should just go for the fastest possible transfers all the time. Get what I paid for.

I'll post back with some code for you to peruse if you feel like it.
Reply
#26
Don't forget that this is an SSD. There are no moving heads so "there is no" delay in accessing the file or its parts. The fragmentation doesn't matter here. Almost. I'm not sure for the small files.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#27
Alright, this is my next version (I'm more happy with it):

import os

def copyFunc(source, destination):
    with open(source, 'rb') as f1, open(destination, 'wb') as f2:
        full_size = os.stat(source).st_size
        full = 0
        increment = 10485760
        chunk = f1.read(increment)
        while f1.read(increment):
            full += increment
            if full + increment > full_size:
                full += full_size - full
            f2.write(chunk)
            print(round(full / full_size * 100, 1), '%\r')

one = '397.64-desktop.exe'
two = 'C:\\Users\Mark\Downloads\Ordenador\driver.exe'

copyFunc(one, two)
The changes you made helped me. I didn't fully understand what was happening behind the scenes with
.read()
and
.write()
. It's good that Python tracks written size vs read size and other stuff pertaining (that I'm not understanding, TBH) to the transfer of file objects. That would be more code if the scripter had to handle him/herself.

But the above code floats my boat, so I'll move on.
Reply
#28
Don't have to be happy. This script doesn't do what you want.
Python doesn't track anything. The script stops because of in the while condition f1.read() finally doesn't return anything.
And you have written to the new destination only the first 10M of the f1. In every loop, chunk is written to f2 but it holds the same data. It never changes.

victor at Jerry in ~
↪ sha1sum '/media/storage/Download/Video/Etcher-Portable-1.3.1-x64.exe'
aea0b63b9bc0881662dd47b036a5f1324f5a6ed3 /media/storage/Download/Video/Etcher-Portable-1.3.1-x64.exe

victor at Jerry in ~
↪ sha1sum '/tmp/Etcher-Portable-1.3.1-x64.exe'
6e455cda41e2a0c4e8954f4776446c59591569c1 /tmp/Etcher-Portable-1.3.1-x64.exe

See?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#29
You're partially right, in my testing. I goofed. The file to be copied does not match the file that was copied. I goofed. But the code is not precisely wrong where you think it is. While there are bytes in the copied file it writes the set chunk. Python must keep track of the stuff somehow. But it's not 100% in my testing (my eyes deceived me when I posted earlier.) All but the last 10MB (increment size) gets copied. I've to go back to the 'lab' and fix it...
Reply
#30
Don't ask me why I have in the Video directory an exe file! Big Grin
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Process finished with exit code 137 (interrupted by signal 9: SIGKILL) erdemath 2 9,384 Apr-18-2022, 08:40 PM
Last Post: erdemath
  Progress bar bnadir55 1 1,781 Apr-11-2022, 01:52 PM
Last Post: deanhystad
  Using .hdf5 files only once they are finished writing pyhill00 7 2,720 Nov-25-2021, 06:01 PM
Last Post: pyhill00
  How to check if a file has finished being written leocsmith 2 7,688 Apr-14-2021, 04:21 PM
Last Post: perfringo
  Progress Indicator for Xmodem 0.4.6 KenHorse 1 1,930 Jan-30-2021, 07:12 PM
Last Post: bowlofred
  process finished with exit code -1073741819 (0xC0000005) GMCobraz 8 5,299 Sep-01-2020, 08:19 AM
Last Post: GMCobraz
  How to stop Xmodem after bin file transfer was finished shaya2103 0 2,473 Nov-27-2019, 04:33 PM
Last Post: shaya2103
  How can I add a progress bar for my software? aquerci 8 3,690 Nov-16-2019, 04:20 PM
Last Post: aquerci
  wget progress bar anasrocks 1 4,690 Jun-06-2019, 03:12 PM
Last Post: heiner55
  Process finished with exit code -107374819 (0xC0000375) mrazko 2 8,394 Apr-05-2019, 12:46 PM
Last Post: mrazko

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020