1

When i try to open a file, even if it is not opened until that moment, it gives an error about that and therefore i cant write on it. Here is my python code:

try:
    myfile = open("SolvedFromFile.xls", "r+")
except IOError:
    mesaj=u"Açık olan nokta listesini kapatın!"
    wx.MessageBox(mesaj, u"UYARI")

What can cause that?

Thanks in advance.

Shansal
  • 1,369
  • 5
  • 16
  • 26
  • 3
    it gives you an error about what? – Mustafa Ekici Jan 05 '12 at 07:09
  • 1
    Remove the exception handling and try again. Your exception handler is hiding our best hope of figuring out what's going on. If you must handle exceptions, please at least use the traceback library (http://docs.python.org/library/traceback.html) to inspect the exception and give useful information. – dyoo Jan 05 '12 at 07:12
  • 1
    Maybe the file doesn't exist?? The file must exist when opening in r+ mode... – Ioan Alexandru Cucu Jan 05 '12 at 07:12
  • 1
    `r+` mode open file for both reading and writing, so if file doesn't exist it will not create a new file instead will give error `IOError: [Errno 2] No such file or directory: 'SolvedFromFile.xls'` – RanRag Jan 05 '12 at 07:13
  • To open excel(.xls) file in python http://stackoverflow.com/q/3239207/776084 – RanRag Jan 05 '12 at 07:14
  • 1
    Unrelated to your issue, but you should use a [with statement](http://docs.python.org/reference/compound_stmts.html#the-with-statement) here – wim Jan 05 '12 at 07:17
  • @Shansal does your code have `'path\to\file.xls'` instead of `'path\\to\\file.xls'` ? – jb. Jan 05 '12 at 07:21
  • I second @wim's comment that you should be using the `with` statement. See the first example [here](http://preshing.com/20110920/the-python-with-statement-by-example). – Chris Jan 05 '12 at 11:52

3 Answers3

1

e.g. you script has no rights to read...rarely happens on your own computer. most seen on productive environment.

or file not found(?)

iloahz
  • 4,491
  • 8
  • 23
  • 31
1

You have to check the caught exception to see what the cause is:

try:
    myfile = open(...)
except IOError as (errno, strerror):
    print 'Error code %d: %s' % (errno, strerror)
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Give the full address in the open() method. Also check if the file exists or not and your have rights to that location. All the best.

Arnab Ghosal
  • 483
  • 1
  • 4
  • 11