Boona
Boona

Reputation: 81

Failed to write to file but generates no Error

I'm trying to write to a file but it's not working. I've gone through step-by-step with the debugger (it goes to the write command but when I open the file it's empty).

My question is either: "How do I see what the error is so I can debug?", "What can go wrong when trying to write to a file that would cause it to behave this way?".

sqlScript = open('script-file.sql', 'a')

    try:
         sqlScript.write(sqlLine)
    except IOError as (errno, strerror):
         print "I/O error({0}): {1}".format(errno, strerror)

This should be simple but I can't seem to find an answer. Also, I apologize in advance as English is a second language.

Edit: I put a print statement just before and the string is not empty.
Edit2: I'm using python 2.6 if that factors in somehow.

Edit 3: I've found a "solution" to my error. I decided to try and run my script using IDLE instead of PyCharm and it works like a charm (pun intended). I have no clue why, but there it is. Any reason why that would happen?!

Upvotes: 7

Views: 9569

Answers (7)

JohnnyC
JohnnyC

Reputation: 1

It happened on my linux environment but work on windows try

sqlScript = open('script-file.sql', 'a', buffering=False)

or

sqlScript = open('script-file.sql', 'a', 0)

Upvotes: 0

Lebugg
Lebugg

Reputation: 313

You have to put your cursor at the beginning of the file. You can do that with seek method:

myScript.seek(0)

See this answer: https://stackoverflow.com/a/2949648/2119117

Upvotes: 2

kindall
kindall

Reputation: 184071

Your current working directory isn't what you expect it to be and it's successfully writing to some script-file.sql in another directory. Try printing os.getcwd() and make sure it's what you expect, and look in that directory.

Upvotes: 0

f p
f p

Reputation: 3223

Are the keywords in the script in lowercase? The same thing happened to me in another db and I solved it by changing words to UPPERCASE.

Upvotes: 0

brettkelly
brettkelly

Reputation: 28205

Building on Chris Morris' answer, maybe do something like this:

try:
    sqlScript.write(sqlLine)
except Exception as e:
    print type(e)
    print str(e)

This will catch any Exception thrown (provided it's a subclass of Exception, of course) and tell you the type and the error message.

Also, it's possible to define multiple except: cases for different possible exceptions, so maybe try doing that for each exception that might be potentially thrown/raised.

Upvotes: 4

user590028
user590028

Reputation: 11730

If no exception is being tossed, I'd suspect the string variable 'sqlLine' is empty.

Did you print it before the write statement?

Upvotes: 0

Chris Morris
Chris Morris

Reputation: 4444

The following code allows you to see what exception it is that is being thrown, and see a trace of where it originated from.

try:
    sqlScript.write(sqlLine)
except:
    print "Unexpected error:", sys.exc_info()[0]
    raise

See http://docs.python.org/tutorial/errors.html for more info.

Upvotes: 2

Related Questions