Author Topic: [Python] Problem with percentage completed progress  (Read 2286 times)

0 Members and 1 Guest are viewing this topic.

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
[Python] Problem with percentage completed progress
« on: May 21, 2013, 06:29:18 pm »
As I am working on crackZip, I want it to show progress in percentage(like 56% completed) which is real time, I mean which is changing as it moves on next line to check but I am not getting ti how to do this.
I implemented a simple formula but the print() isn't printing in the screen.
Code: (python) [Select]
total = 0
for lines in dictionary.readlines():
      total += 1
line = 0
for lines in dictionary.readlines():
    line += 1
    print("Testing...     %.3f% completed" % (line * 100 / total))

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: [Python] Problem with percentage completed progress
« Reply #1 on: May 21, 2013, 06:41:36 pm »
How large is your dictionary?
You are reading the whole dictionary before you even start using it. That's a bad idea. I suppose it takes long because it needs time to read the dictionary.
It could be something else, but then I need to see more code.

Edit: Use the size of the file instead of the number of lines. The size is something you get instantly. To get the number of lines you always have to parse the whole file.
« Last Edit: May 21, 2013, 06:43:25 pm by Deque »

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: [Python] Problem with percentage completed progress
« Reply #2 on: May 22, 2013, 07:24:53 am »
How large is your dictionary?
You are reading the whole dictionary before you even start using it. That's a bad idea. I suppose it takes long because it needs time to read the dictionary.
It could be something else, but then I need to see more code.

Edit: Use the size of the file instead of the number of lines. The size is something you get instantly. To get the number of lines you always have to parse the whole file.
The dictionary can be as large as you want, 2GB 15GB or even 150GB.
As for the size of the file, I didn't get how to calculate percentage by using size of the file instead of counting the lines. You are right as it'll take time to read the dictionary.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: [Python] Problem with percentage completed progress
« Reply #3 on: May 22, 2013, 09:07:15 am »
The dictionary can be as large as you want, 2GB 15GB or even 150GB.
As for the size of the file, I didn't get how to calculate percentage by using size of the file instead of counting the lines. You are right as it'll take time to read the dictionary.

That's what I meant. It takes a long time to read and parse 2GB files for newlines. I didn't want to know how large I can make my files, I wanted to know how large yours is that you use for testing the program.

 
Quote
I didn't get how to calculate percentage by using size of the file instead of counting the lines

Instead of counting the lines count the bytes you got.
« Last Edit: May 22, 2013, 09:08:43 am by Deque »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: [Python] Problem with percentage completed progress
« Reply #4 on: May 22, 2013, 09:29:03 am »
I wrote an example code that will help you:

Code: (Python) [Select]
import sys
import os

path = "text"
file_size = os.stat(path).st_size
f = open(path, 'r', 1)
bytes_read = 0
for line in f:
    bytes_read += len(line)
    percent = bytes_read * 100 / file_size
    print "percentage: ", percent, "%"
print
print "bytes_read:", bytes_read
print "file_size:", file_size

This way you can still read the file line by line but also keep track of the bytes read so far.

Example output:

Code: [Select]
percentage:  15 %
percentage:  15 %
percentage:  43 %
percentage:  43 %
percentage:  47 %
percentage:  47 %
percentage:  56 %
percentage:  56 %
percentage:  74 %
percentage:  74 %
percentage:  77 %
percentage:  77 %
percentage:  82 %
percentage:  82 %
percentage:  100 %

bytes_read: 2749
file_size: 2749
« Last Edit: May 22, 2013, 09:32:18 am by Deque »

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: [Python] Problem with percentage completed progress
« Reply #5 on: May 22, 2013, 10:01:34 am »
I want to print it in one line which is updating in real time and I don't want to print the 'percentage complete' in every line.
Any way to do that?

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: [Python] Problem with percentage completed progress
« Reply #6 on: May 22, 2013, 10:32:57 am »
Use a carriage return:

Code: (Python) [Select]
import sys
import os
from time import sleep

path = "text"
file_size = os.stat(path).st_size
f = open(path, 'r', 1)
bytes_read = 0
for line in f:
    bytes_read += len(line)
    percent = bytes_read * 100 / file_size
    sys.stdout.write("\rpercentage: " + str(percent) + "%")
    sys.stdout.flush()
    sleep(1)
print
print "bytes_read:", bytes_read
print "file_size:", file_size

Note: I put the sleep(1) in there for demonstrating purposes. Otherwise it would be too fast for my small testfile.

You might also use the progress bar made by 3vilp4wn, you find it here (last post):
http://evilzone.org/java/%28java-snippet%29-cli-animated-progress-bar/msg57524/#new
« Last Edit: May 22, 2013, 10:36:13 am by Deque »

Offline pyte

  • Peasant
  • *
  • Posts: 79
  • Cookies: -7
    • View Profile
Re: [Python] Problem with percentage completed progress
« Reply #7 on: May 23, 2013, 07:38:14 am »
I tried modifying your code with an intention to delete every file that i found to be below 250 bytes and also over 24 hours from the last time it was modified but im getting an error here.
forgive me if this is a silly one. im just not getting leads even from Google.


Code: (Python) [Select]
import sys
import os
from time import sleep
from datetime import datetime
 
path = "/home/pytbyte/Documents/cooperates contactslist.docx"
file_size = os.stat(path).st_size
f = open(path, 'r', 1)
bytes_read = 0
for line in f:
    bytes_read += len(line)
    percent = bytes_read * 100 / file_size
    sys.stdout.write("\rpercentage: " + str(percent) + "%")
    sys.stdout.flush()
    sleep(1)
print
print "bytes_read:", bytes_read
print "file_size:", file_size


file_modified = datetime.datetime.fromtimestamp(os.path.getmtime(curpath))
if datetime.datetime.now() - file_modified > datetime.timedelta(hours=24):
    os.remove(curpath)
if os.path.getsize(fullpath) < 200 * 1024:
    os.remove(fullpath)
« Last Edit: June 27, 2013, 07:32:12 pm by RedBullAddicted »
If you don't go into the tiger's cave, how will you get the cub?

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: [Python] Problem with percentage completed progress
« Reply #8 on: May 23, 2013, 09:14:26 am »
I tried modifying your code with an intention to delete every file that i found to be below 250 bytes and also over 24 hours from the last time it was modified but im getting an error here.
forgive me if this is a silly one. im just not getting leads even from Google.


Code: (Python) [Select]
import sys
import os
from time import sleep
from datetime import datetime
 
path = "/home/pytbyte/Documents/cooperates contactslist.docx"
file_size = os.stat(path).st_size
f = open(path, 'r', 1)
bytes_read = 0
for line in f:
    bytes_read += len(line)
    percent = bytes_read * 100 / file_size
    sys.stdout.write("\rpercentage: " + str(percent) + "%")
    sys.stdout.flush()
    sleep(1)
print
print "bytes_read:", bytes_read
print "file_size:", file_size


file_modified = datetime.datetime.fromtimestamp(os.path.getmtime(curpath))
if datetime.datetime.now() - file_modified > datetime.timedelta(hours=24):
    os.remove(curpath)
if os.path.getsize(fullpath) < 200 * 1024:
    os.remove(fullpath)

Those are so many mistakes, I suggest you learn programming instead of copy&pasting codes together without knowing what happens there.

However, I will tell you why it doesn't even run properly.
You use variables like curpath and fullpath that are not even defined. I can't tell you what they should be, only you can know.
You import the member datetime from datetime, yet you try to get the member datetime from datetime again. Doesn't make sense. Either do just "import datetime" or correct the calls "datetime.datetime.x" to just "datetime.x"

If you did all that it will run, but still not work as you intended. You just clashed some code pieces to getter that don't fit. That's not how you can achieve anything.

Also: Remove the sleep(1)
It was just for testing purposes as stated above.
« Last Edit: June 27, 2013, 07:32:31 pm by RedBullAddicted »