Some python questions (building cgi website)

P

paul h

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.

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?


any help with these questions would be great!
cheers,
paul
 
A

Alex Martelli

paul said:
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.

Assuming that:
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
directory;
4. some variable, say X, refers to the string which in this
case happens to be 'HELLO';

then:

import os
os.mkdir(X)

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!!!


Alex
 
M

Matt Goodall

paul said:
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.
See os.mkdir() or os.makedirs().

Please be *very* careful letting users access the file system in this
way. Consider the situation where the user enters "/HELLO",
"../../HELLO" etc - they could do all sorts of damage! You should
probably restrict directory creation to a certain area of the file
system. os.path.normpath() can be useful in working out what the user's
input really means.
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?
Unless you have created an object reference called file then file is
actually a builtin for *opening* a file, or rather for creating a file
object.

This is probably what you want:

file('thefile.txt','wt').write('TEXT HERE')

or better still

f = file('thefile.txt','wt')
try:
f.write('TEXT HERE')
finally:
if f:
f.close()

which will ensure the file is closed correctly.

Cheers, Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top