Some python questions (building cgi website)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • paul h

    Some python questions (building cgi website)

    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("TEX T HERE")
    how can i write plain text into a file?


    any help with these questions would be great!
    cheers,
    paul
  • Alex Martelli

    #2
    Re: Some python questions (building cgi website)

    paul h wrote:
    [color=blue]
    > 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.[/color]

    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 administrativel y or programmaticall y
    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.

    [color=blue]
    > i also need to know how i can write various things into a file.
    > the following doesnt seem to work for me:
    > file.write("TEX T HERE")
    > how can i write plain text into a file?[/color]

    You need to OPEN a file for writing first:

    x = file('thefile.t xt', '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

    Comment

    • Matt Goodall

      #3
      Re: Some python questions (building cgi website)

      paul h wrote:
      [color=blue]
      >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.
      >
      >[/color]
      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.normpat h() can be useful in working out what the user's
      input really means.
      [color=blue]
      >i also need to know how i can write various things into a file.
      >the following doesnt seem to work for me:
      >file.write("TE XT HERE")
      >how can i write plain text into a file?
      >
      >[/color]
      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.t xt','wt').write ('TEXT HERE')

      or better still

      f = file('thefile.t xt','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@pollenatio n.net



      Comment

      Working...