Some python questions (building cgi website)
aleax at aleax.it
Thu Nov 6 12:40:39 CET 2003
paul h wrote:
> hi there,
> i'm learning to use python for cgi, and i've hit a few road blocks.
> basically, i need to know how to achieve the following things using
> python, on linux.
> create a new directory, and sub-directory.
> so if the user inputs "HELLO", i can create a dir called HELLO, and
> then some other dirs in there.
1. your process's current directory is the one you want to
have as the parent of 'HELLO';
2. your process is running as a user that has permission to
write in its current directory;
3. there is no subdirectory named 'HELLO' in the current
4. some variable, say X, refers to the string which in this
case happens to be 'HELLO';
will make that 'HELLO' subdirectory, and then, e.g.:
os.mkdir(X + '/foop')
will create a subdirectory foop of that HELLO subdirectory.
All these points, especially 1-2, cannot be taken for granted
since your process is running as a CGI script; you must ensure
by administrative means that your process is running as the user
you desire (Python or any other language is powerless to help
you there!) and either administratively or programmatically
ensure the current directory is the one you want (or that you
know the full path to prepend to X) -- the latter may not be
trivial if chroot is in play, as it should be in most well-run
network servers, for security reasons.
> i also need to know how i can write various things into a file.
> the following doesnt seem to work for me:
> file.write("TEXT HERE")
> how can i write plain text into a file?
You need to OPEN a file for writing first:
x = file('thefile.txt', 'w')
and THEN you can call x.write("TEST HERE") to write some string
(in this case, one without a terminating newline). file.write
is an unbound method of built-in type file and, called as you
mean to, would have no possible idea on WHAT file to write on!!!
More information about the Python-list