Some python questions (building cgi website)
Matt Goodall
matt at pollenation.net
Thu Nov 6 06:48:35 EST 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.
>
>
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
--
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenation.net
e: matt at pollenation.net
More information about the Python-list
mailing list