4

I try to open a file for writing using the following:

fh = open("testfile", "w")

But, I get an exception:

IOError: [Errno 17] File exists!

I have write permissions and there is no race condition. Does anyone know why I get the error?

Thanks.

Linus Caldwell
  • 10,908
  • 12
  • 46
  • 58
Green
  • 41
  • 1
  • 3
  • 4
    This is really an OS error, so you should mention your OS and anything possibly unusual? Is it a network share? Removable disk? etc. – John La Rooy Jun 10 '13 at 22:21
  • 1
    Related question: http://stackoverflow.com/questions/13317389/python-exception-errno-17-file-exists-using-open-on-windows – Rushy Panchal Jun 10 '13 at 22:22
  • 1
    what happens if you try to open the file in append mode? – Levon Jun 10 '13 at 22:22
  • 1
    It doesn't have an answer because it isn't a Python problem. It's a Windows exclusive file access problem and it has no reliable solution. – msw Jun 10 '13 at 22:29
  • 2
    @msw, This error can happen if unix/linux if the existing "file" is a directory or special file. I guess you are saying it's possible to see it on Windows even if the existing file is a regular file. Do you have a link to more information about this? – John La Rooy Jun 10 '13 at 22:43
  • I use AIX operating system. The file does exist. It is ok if I open it with 'r+'. – Green Jun 10 '13 at 22:46
  • It is a status file, I update this file muultiple times in my program using the same file writting method. Sometimes, it is successful, sometimes it fails at the file open. – Green Jun 10 '13 at 22:48
  • Why "file exists" is an error? It should be ok if the file exists. – Green Jun 10 '13 at 22:48
  • `fopen()` should never return `EEXIST` according to [the man page](http://pic.dhe.ibm.com/infocenter/aix/v7r1/index.jsp?topic=%2Fcom.ibm.aix.basetechref%2Fdoc%2Fbasetrf1%2Ffopen.htm)... – Ignacio Vazquez-Abrams Jun 10 '13 at 23:01
  • If we use "wx" option and the file exists, then it will return "file exists" error. – Green Jun 10 '13 at 23:37
  • I tried to use "w+" option, and it works. Not sure the reason. – Green Jun 11 '13 at 01:01

1 Answers1

0

The wx mode is something new in Python 3.3, and is documented to raise an error if the file already exists. If w+ mode is working, it seems to me that the code published in the question is not really in line with the error returned...

Joël
  • 2,723
  • 18
  • 36