[Tutor] Creating Binary Files of 1MB
Steven D'Aprano
steve at pearwood.info
Sun Apr 17 09:34:18 CEST 2011
Becky Mcquilling wrote:
> I'm trying to work out a method to create a binary file of 1mb and I'm
> somewhat confused by the docs I'm reading is there a method that will do
> this?
# Open a file in binary (b) mode for writing.
fp = open("mybinaryfile", "wb")
# Write whatever data you like to it.
fp.write('\0'*1000000)
fp.close()
That writes one million ASCII NULL bytes to the file. If you prefer
binary megabytes instead of decimal megabytes, use 1024*1024 instead of
one million.
If you want some other binary data, you'll have to tell us what binary
data you want to write.
--
Steven
More information about the Tutor
mailing list