Failed to write to file but generates no Error

Go To


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')

    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?!

2012-04-04 19:28
by Boona
What do you mean by "it's not working"? Does it generate the exception you're catching? Or is no exception generated - alan 2012-04-04 19:32
Did you close or flush the file in the Python process prior to inspecting it - Fred Foo 2012-04-04 19:32
Alan: It doesn't generate an error. Larsmans: I only open it to append - Boona 2012-04-04 19:44
Is this your actual code? Given the wrong indentation, I suspect not and that something else is affecting it. Have you tried running your code exactly as it is posted here (set sqlLine a value yourself)? It could be the problem lies elsewhere. You should also look into the <code>with</code> statement for writing files (with open('script-file.sql', 'a') as sqlScript:) - Gareth Latty 2012-04-04 20:18
It's the actual code (only I changed the file name since it's a client's name.) It didn't keep my indentation when I copied it over so I put spaces myself - Boona 2012-04-04 20:29


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

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.

2012-04-04 19:36
by inkedmn
Thanks for your help, but still no error - Boona 2012-04-04 19:49


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

    print "Unexpected error:", sys.exc_info()[0]

See for more info.

2012-04-04 19:32
by Chris Morris
I used your code and I don't get an error. It's really strange - Boona 2012-04-04 19:41
@Boona Are you sure? That seems pretty weird. No 'unexpected error' messages pops up on your console - Chris Morris 2012-04-05 00:37


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

See this answer:

2014-10-24 10:23
by Lebugg


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

Did you print it before the write statement?

2012-04-04 19:35
by user590028
I put a print statement just before and the string is not empty - Boona 2012-04-04 19:41


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.

2012-04-04 19:58
by f p
I don't even get a chance to run the script since I can't write it to the file. :P (Note: I'm not writing directly to the DB but generating a script that will be run at a later date. - Boona 2012-04-04 20:01
Can you show something from the contents of sqlLine - f p 2012-04-04 20:27
I just found the issue. When I tried running it with IDLE instead of PyCharm it worked. I have no idea why. : - Boona 2012-04-04 20:49


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.

2012-04-04 19:59
by kindall
Interesting! I checked and it's the directory I was expecting - Boona 2012-04-04 20:04


It happened on my linux environment but work on windows try

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


sqlScript = open('script-file.sql', 'a', 0)
2016-11-18 21:00
by JohnnyC