[Tutor] creating files with python and a thanks

Sheila King sheila@thinkspot.net
Sat, 16 Feb 2002 06:37:55 -0800

On Sat, 16 Feb 2002 08:06:19 -0600, "Cameron Stoner"
<wolf_binary@hotmail.com>  wrote about [Tutor] creating files with
python and a thanks:
> How can you tell Python to make a simple text file or to make a kind of file 
> with your own kind of extension?  I have looked around in books I have, and 
> online.  I have only found stuff about how to open and close and recieve 
> input from.  I don't know enough about file creation and operating systems 
> to know I guess how to tell Python to do it.

But the opening and closing stuff IS how you make files.

OK, what do you want in your file?

Let's say, you want your file to contain the following data:

this is my file
my stuff
is in

and let's say you want to save in on your computer under the name


Then this code should work:

f = open('myfile.mbg', 'w')
f.write('this is my file\nmy stuff\nis in\nmy\nfile')

Because you are opening the file with a 'w' flag, it will let you write
stuff into the file. The file will end up in whatever the current
directory is. If you're not sure where that is, you could amend the
script like this, so that is will tell you what directory it is saving
the file in:

import os

f = open('myfile.mbg', 'w')
f.write('this is my file\nmy stuff\nis in\nmy\nfile')
print "current directory is ",
print os.path.abspath(os.getcwd())

Look in that directory and you should find your newly created file.

If you want to save the file in a specific directory, you could do this

f = open(r'c:\windows\temp\myfile.mbg', 'w')
f.write('this is my file\nmy stuff\nis in\nmy\nfile')

(I used the r in front of the quotes, so that it will interpret the
string as a "raw" string, and not assume the \t on \temp represents the
escape sequence for a tab character.)

What if later you want to add more stuff to the file, you can open it in
append mode.

f = open('myfile.mbg', 'a')
f.write('Here is\nsome more stuff\nfor\nmyfile')

Now when you open the file, it will have these contents:

this is my file
my stuff
is in
fileHere is
some more stuff
my file

Opening the file with the 'a' flag let's you append to it. If you open
it with the 'w' flag, it will erase anything that was previously in the
file and start it over from scratch.

What if you want to write binary files? How about, you have some .gif
file called some.gif
and you want to make a copy of it called somecopy.gif

This should do it:

blocksize = 1024
origfile = open('some.gif', 'rb')
newfile = open('somecopy.gif', 'wb')
while 1:
	datachunk = origfile.read(blocksize)
	if not datachunk:

Instead of reading the entire .gif file into memory, you might want to
consider reading only a small "chunk" at a time, so you don't clobber
the memory on your computer.

The 'rb' and 'wb' flags are for reading binary files and writing binary
files. (Different operating systems have different line endings, and in
text files Python will "do the right thing" and use the appropriate line
ending. This is great in text files. But in binary files, you don't want
Python changing any of the bytes in the file, so opening with a binary
flag tells Python not to make any line ending translations, but just to
read or write EXACTLY the data that it is given.)

Hope this gets you started on creating your own files!

(P.S. I was a little lazy, and didn't run any of the code presented here
through the interpreter. If you get syntax errors you can't figure
out...by some stroke of bad luck, let me know and I'll test the code and
fix it. I think it should be OK?)

Sheila King

"When introducing your puppy to an adult cat,
restrain the puppy, not the cat." -- Gwen Bailey,
_The Perfect Puppy: How to Raise a Well-behaved Dog_