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

S A buc40@bemail.org
Wed, 21 Aug 2002 12:23:33 -0700


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

>    newpyfile = "newdesign/papers/cgi/" + refname + ".py"
>    newpy = open(newpyfile, "w")
>    newpage = """
>          <h2 align = 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=center>"""+query["cot"].value
>    newpy.write(newpage)
>    newpy.close()
>    newhtmlfile = "newdesign/papers/papers/" + refname + ".html"
>    newhtml = open(newhtmlfile, "w")
>    newwpage = """
>        <!--#include virtual="../../template/head.shtml" -->
>        <!--#exec cgi="../cgi/""" + refname + """.py-->
>        <!--#exec cgi="../../template/footer.shtml" -->"""
>    newhtml.write(newwpage)
>    newpy.close()
>
Here is the offending section. When you ran this from the command line, were you already in the correct directory?

Because this program uses an incomplete path to the target directory. Therefore, if the script is not run from the root of this path, it will not find the correct directory hence, the error. (I think this is your problem anyways)  When the program is run, python treats the directory the script was run in as it's starting point. For example:

If you ran this script from /scripts/:

The program works from the /scripts directory and looks for newdesign/papers/papers/ in that directory. So if this is not present you will get the error.

When writing the file in from the command line, it probably greated the new file in '.' (or current directory) so you did not get the error.

Try using absolute path names in your script and see if that fixes the problem,

Good Luck.
SA



"I can do everything on my Mac that I used to do on my PC, plus alot more ..."

-Me

------------------------------------------------------------
Free, BeOS-friendly email accounts: http://BeMail.org/
BeOS News and Community: http://www.BeGroovy.com/


---------------------------------------------------------------------
Express yourself with a super cool email address from BigMailBox.com.
Hundreds of choices. It's free!
http://www.bigmailbox.com
---------------------------------------------------------------------