creating a file from python

Steve Holden sholden at holdenweb.com
Sat Jul 6 14:54:14 EDT 2002


"gollem" <gollem at chatway.nl> wrote ...

> I want to create a file from inside a running python script.

Dear gollem:

This sounds like something Python should be able to take in its stride.
Python programs all around the world are doing this every day, and Python
was invented precisely to give us new and more interesting ways to do the
same thing. It still is ;-)

> I can do that using commands and touch but it won't work if the intended
> name of the file is the value of a python variable.

I'm going to make an assumption here, so ignore the rest of this if I'm
wrong: I presume you mean you can enter "though" commands into a Unix (or
cygwin) command window, but you aren't sure how to do similar things from
inside Python scripts.

You may or may not know that Python's "os" module has a "system" function
that lets you pretty much (though not *exactly*) "type a command". Of
course, in Python there are dozens of ways you can do this. An example for
the security-conscious:

    import os
    command = input("Any command you like:")
    result = os.system(command)

In other words, if you can build it as a Python string, you can use it as a
command. So, if you have some of the bits of the commands you need in Python
strings already, you can use the names those strings are bound to in
string-values expressions that you use (or intend to use) as arguments to
os.system().

> The file is created as
> the variable name instead of the variable value.

If you don't mean a Python variable here, I'm lost, but I suppose you
*might*  mean a shell variable. Suppose you wanted to copy a file, you might
use code like this:

    filename = "fileToCopy.txt" # "puts filename in variable"
    # in Python, we talk about binding names to values.
    # the name "filename" is now bound to "fileToCopy.txt".
    command = "cp "+filename+" /tmp/saved"
    result = os.system(command"

> I think I need to use a shell (bash) variable to do it but then the
question
> becomes how do I create that? thnx
>
If you want to use the values of shell environment variables, again Python
has help readily available as os.environ. This is a dictionary of your
environment, and can be used to access your environment, and also to modify
the environment that any sub-processes use. The value of the HOME
environment variable is readily available as

    os.environ["HOME"]

for example, so if you wanted to copy the file above into a subdirectory of
your home directory you might use

    command = "cp "+filename+" "+os.environ["HOME"]+"/saved"

rather than the hard-wired /tmp directory: the command now works for each
user based on their home directory.

To read and write a file from a Python program you again need the filenames.
Look up the file() or open() functions to see how and maybe get a couple of
examples.

regards
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------








More information about the Python-list mailing list