EvilZone
Programming and Scripting => Scripting Languages => Topic started by: parad0x 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.
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))
-
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.
-
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.
-
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.
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.
-
I wrote an example code that will help you:
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:
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
-
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?
-
Use a carriage return:
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 (http://evilzone.org/profile/?u=12943), you find it here (last post):
http://evilzone.org/java/%28java-snippet%29-cli-animated-progress-bar/msg57524/#new (http://evilzone.org/java/%28java-snippet%29-cli-animated-progress-bar/msg57524/#new)
-
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.
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)
-
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.
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.