Quick Python question...

Post » Mon Jul 15, 2013 11:45 am

Alright, if you don't know what Python is, then this probably won't make much sense.

So my goal is to make a 1GB text file with Python.

Problem?

I started learning Python about an hour ago.

So at first I thought

a = "0"b = 1024print(a + b)input("Press Enter to continue...")

But that didn't work.

So how could this be accomplished? Maybe make Python do the math using 0's, and then output the results to a text file?

User avatar
CYCO JO-NATE
 
Posts: 3431
Joined: Fri Sep 21, 2007 12:41 pm

Post » Mon Jul 15, 2013 2:52 am

I don't know Python myself, but here's the idea:

You divide 1 GB by the size of the character type. This number gives you the number of characters you need to make 1 GB. You then have a loop run this many times, spitting out some character to the file.
User avatar
Emily Shackleton
 
Posts: 3535
Joined: Sun Feb 11, 2007 12:36 am

Post » Mon Jul 15, 2013 1:43 pm

I hate snakes!

But really, I'm good at everything but Math. Sorry.
User avatar
BRAD MONTGOMERY
 
Posts: 3354
Joined: Mon Nov 19, 2007 10:43 pm

Post » Mon Jul 15, 2013 12:31 am

Thanks fellas.

Alright, I found a little more...

a = "0"b = 1024print(a * b)input("Press Enter to continue...")

This will get me 1KB of 0's... But, I can't copy/paste them.

???

User avatar
naana
 
Posts: 3362
Joined: Fri Dec 08, 2006 2:00 pm

Post » Mon Jul 15, 2013 1:02 pm

It sounds like it's working in bytes, then. 1 GB = 1024 MB. 1 MB = 1024 KB. 1 KB = 1024 Bytes.

So, you multiply through.
User avatar
Ridhwan Hemsome
 
Posts: 3501
Joined: Sun May 06, 2007 2:13 pm

Post » Sun Jul 14, 2013 11:32 pm

There's this: http://stackoverflow.com/questions/3377891/how-do-i-efficiently-fill-a-file-with-null-data-from-python/3377909#3377909

So you could do:

with open("file.to.create", "wb") as out:    out.seek((1024 * 1024 * 1024) - 1)    out.write('\0')

Which would create a 1GB file filled with a bunch of null characters. But, the file system you are using would need to support sparse files (http://en.wikipedia.org/wiki/Sparse_file). (I'm not sure if windows does, but it works on my machine)

User avatar
Georgia Fullalove
 
Posts: 3390
Joined: Mon Nov 06, 2006 11:48 pm

Post » Mon Jul 15, 2013 10:46 am

Mainly now, I need a line to output the file to a copy/pasteable format, or just too the text file I need to create. Not just to the command line box.

Thanks Death, I'll use that.

User avatar
suniti
 
Posts: 3176
Joined: Mon Sep 25, 2006 4:22 pm

Post » Mon Jul 15, 2013 3:47 am

The operation "> somefile.txt" dumps any commandline output to file somefile.txt in current directory.

Or you can have python create the file for you, which'll be faster, though you should learn try/catch before doing file operations.
User avatar
Elea Rossi
 
Posts: 3554
Joined: Tue Mar 27, 2007 1:39 am


Return to Othor Games