Can't Write File

Victor Subervi victorsubervi at gmail.com
Wed Nov 11 09:00:44 EST 2009


On Tue, Nov 10, 2009 at 4:33 PM, Dave Angel <davea at ieee.org> wrote:

> Victor Subervi wrote:
>
>> Hi;
>> I've determined the problem in a script is I can't open a file to write
>> it:
>> script = open(getpic, "w")  # where getpic is already defined
>> Here are the permissions:
>> -rwxr-xr-x  1 root root  4649 Nov 10 12:31 start.py
>> What am I doing wrong?
>> TIA,
>> Victor
>>
>>
>>
> Wrong?
>
> 1) you don't specify the environment, python version, OS, etc.
>
python 2.4.3
CentOS 5.4 final

> 2) you don't show the error traceback
>
because there are none

> 3) you don't indicate the value of   getpic at the time this statement
> executes
>
As mentioned earlier, it increments:
getpic1.py
getpic2.py
getpic3.py
getpic4.py
getpic5.py

> 4) you don't indicate what the current working directory is, os.getcwd()
>
/var/www/html/angrynates.com/stxresort/cart

> 5) you don't indicate what directory the start.py is located in
>
ditto

> 6) you don't indicate which user is executing this script (only root can
> write to it)
>
Help me on this. All scripts are owned by root. Is it not root that is
executing the script?

> 7) you don't tell what the filename of the script is, specif. if it's
> called start.py

yes, start.py

> 8) you don't say what happens if you do an open(getpic, "r"), or
>  os.path.isfile(getpic)
>
It doesn't like either of those. Goes to the except in the try.

By the time you answer each of those questions, you may notice the answer to
> your own question.
>

I wish that were so. I have written a little script that does nothing but
test the line in question [open(getpic, 'w')] and that works. Here's a
bigger code snippet with the entire try clause:

          if 14 < x < 20: # This just shows that it's a pic, not some other
type of data
            y += 1
            w += 1
            try: # It does this, because I've printed 'getpic1.py' etc.
              getpic = "getpic" + str(w) + ".py"
              try:
                os.remove(getpic)
              except:
                pass
              code = """#! /usr/bin/python
import cgitb; cgitb.enable()
import MySQLdb
import cgi
import sys,os
sys.path.append(os.getcwd())
from login import login
def pic():
  user, passwd, db, host = login()
  form = cgi.FieldStorage()
  db = MySQLdb.connect(host, user, passwd, db)
  cursor= db.cursor()
  sql = "select pic%s from products where ID=%s;"
  cursor.execute(sql)
  content = cursor.fetchone()[0]
  cursor.close()
  print content
print 'Content-type: image/jpeg'
print
pic()""" % (str(w), str(i))
              script = open(getpic, "w") # I've deleted everything below
this line to the except to test
              script.write(code)
              print '<td><input type="hidden" name="%s" />\n' % str(x)
              os.chmod(getpic, 0755)
              print '<img src="%s" width="100"><br /><br /></td>\n' %
(getpic)
              count2 += 1
            except:
              print '<td>nope</td>'
          else:
            print '<td>%s</td>\n' % (field)
          x += 1


TIA,
V
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091111/74d39817/attachment.html>


More information about the Python-list mailing list