[Tutor] creating files and writing to them

Tim Peters tim_one@email.msn.com
Thu, 3 Jun 1999 04:16:38 -0400


[Todd Martin]
> Ok so I can do these things in perl and shell but I'm just now learning
> python and am a little confused.

That won't last -- you're doing fine!

> What I want to do is pass a username to a script as an argument like so:
>
> % script username
>
> Then write a file named "username.startup".

No problem.  You'll find the arguments to your script in Python's sys.argv.
That is,

import sys
print sys.argv

It's a list of strings, very much like you're used to from Perl.  Except
Python generally gives sensible *names* to things <wink>.

> This is where I'm at right now:
>
> #! /usr/bin/python
>
> file = open('/home/todd/docname', 'w')
> file.write('The create script in its infancy\n')
> file.close()
>
> def docname ():
>         username = "todd"
>         startup = ".startup"
>         doc = username + startup
>         print "%s" % doc

That last line is easier as

    print doc

and does the same thing.  Python automatically converts the expressions in a
"print" stmt to strings for you, and doc started life as a string anyway.

> docname()
>
> I know its lame so far but I'm new :)

Not lame at all:  that's how you start!  Get something (anything) that
works, and play with it.  Believe it or not, you're almost done with it.

> Ok so that last tidbit there is how I figured I could take the
> username and  append ".startup" to it. But now I need to create a file
> by that  name, rather than the way its being done now, where I have to
> declare the file name in advance (does that make sense?).

Yes, you want username to be taken from the command line, instead of
hardcoded.  So, assuming you did

import sys

at the top of your program, just change your username definition to

        username = sys.argv[1]

Then username will become whatever string you passed as the first argument
on the command line.

> Now I know I will need to pass the username argument to argv, but
> that can wait untill I find out how to pass file = open() a
> variable rather than a file name.

open() simply wants a string, and will accept any *expression* that
*evaluates* to a string.  A string literal like you have is one kind of
expression that evaluates to a string.  You could have written

file = open('/home/todd/docname', 'w')

as

somename = '/home/todd/docname'
file = open(somename, 'w')

instead, or as

def somename():
    return '/home/' + 'todd/' + 'doc' + 'junkname'[-4:]
file = open(somename(), 'w')

or in a million other ways.

So try moving your docname function to the top of your file (so that your
function gets defined before you try to call it!), change it to return doc
instead of just printing it, and replace your literal filename with a call
to docname:

file = open(docname(), 'w')

Python doesn't have a lot of special syntax to learn (and especially not
compared to Perl), and the general rule is it doesn't matter how you spell
things just as long as the expressions you pass to functions evaluate to the
*types* of things the function expects.  Try open(3) or even open(open).
What happens?  Python will yell at you when it can't make sense out of what
you're doing.  This is also unlike Perl, although running Perl with -w is
much closer to Python.

most-of-all-have-fun-ly y'rs  - tim