[Tutor] write to a file in linux (again)!

Watt III, Glenn gwatt3@backbonesecurity.com
Wed, 21 Aug 2002 10:45:23 -0400


-----Original Message-----
From: Tim Wilson [mailto:wilson@visi.com]
Sent: Tuesday, August 20, 2002 4:06 PM
To: Watt III, Glenn
Cc: tutor@python.org
Subject: Re: [Tutor] write to a file in linux
>
>Try this:
>
>import os
>wrt =3D open("filename", "w")
>wrt.write('Here is the text to write.')
>wrt.close()
>
>In your example 'wrt' is called a file handle. Any manipulating of the
>file occurs by specifying the name of the file handle first (See the
>write command above). You'll see a lot of examples that use 'f' as the
>file handle name.

ok i did this and it works in the shell but whenever i implement it in
the script im running i get this error

Traceback (most recent call last): File
"/var/www/backbone/admin/newdesign/papers/cgi/final.py", line 58, in ?
newpy =3D open(newpyfile, "w") IOError: [Errno 2] No such file or
directory: 'newdesign/papers/cgi/1.py'=20

well of coarse its not a file or directory im creating a new one how do
i tell it to create a new one as a script it didnt need any thing extra
for the shell just for reference here is the script

#!/usr/local/bin/python

import cgi
import sys
import MySQLdb
import os

connection =3D MySQLdb.Connect(login info)
cursor =3D connection.cursor()

if query.has_key("accept"):
    header =3D "Content-type: text/html\n"
    print header
    print """
       <html>
        <head>
        <title>Popsiclestick.com</title>
        </head>
        <body>"""
else:
    print "Well that was kind of useless you didn't even decide whether
or not to accept"
    sys.exit(0)

if query['accept'].value =3D=3D "Yes":
    num =3D 1
    x =3D "No"
    while x =3D=3D "No":
        cursor.execute("select state from newpapers")
        for link in cursor.fetchall():
                if link[0] =3D=3D num:
                        num =3D num + 1
              else:
                        x =3D "yes"

    refname =3D str(num)
    newpyfile =3D "newdesign/papers/cgi/" + refname + ".py"
    newpy =3D open(newpyfile, "w")
    newpage =3D """
          <h2 align =3D center>Recently submited papers</h2>
           Name: """+query["title"].value+"""
          <br>Web Site: """+query["web"].value+"""
          <br>Location: """+query["city"].value+"""
          <br>Title: """+query["papertitle"].value+"""
          <p align=3Dcenter>"""+query["cot"].value
    newpy.write(newpage)
    newpy.close()
    newhtmlfile =3D "newdesign/papers/papers/" + refname + ".html"
    newhtml =3D open(newhtmlfile, "w")
    newwpage =3D """
        <!--#include virtual=3D"../../template/head.shtml" -->
        <!--#exec cgi=3D"../cgi/""" + refname + """.py-->
        <!--#exec cgi=3D"../../template/footer.shtml" -->"""
    newhtml.write(newwpage)
    newpy.close()

it goes on from there but this is the import part right now