1

I'm facing a strange issue using the standard open(path, "w") function to write to a file. It works as expected on my mac, but I tried my script on VM running Windows 7 and I get "[Errno 17] File exists" exception... this is driving me crazy since I really don't understand... I perfectly know that the file exists, and in fact is my intention open it and write content into it... can you explain me where the problem may comes from?

ps. I also checked that I have the permission to write to the file using os.access(path, os.W_OK)

uh... on my Mac I'm using Python 2.7 and 3.3 on the VM... but the official docs don't report any significant change in the function :P

UPDATE: After refactoring my code in order to remove threads (since I thought they may be the problem), I get a "permission denied error"... the file is not used by any other process since I have simulated that scenario by getting a "permissionError: the process cannot access the file because it is being used by another process". My python program is running as a pydev build in Eclipse... if I open a shell and I run the command f = open(thefile, "w") no error occurs. The Eclipse workspace is located under user documents... so I have the necessary rights... what should be? I'm really stuck :( (on my mac os x all works perfectly even after switching to python 3.3 and code refactoring)

msw
  • 42,753
  • 9
  • 87
  • 112
daveoncode
  • 18,900
  • 15
  • 104
  • 159
  • 2
    Is it possible that some other process has the file open? On Windows, you can open a file for exclusive access—and most apps do so by default. You can get a command-line tool called `Handle` at http://technet.microsoft.com/en-us/sysinternals/bb795533 that's sort of like POSIX `lsof`, if you want to find out who has the file open. – abarnert Nov 09 '12 at 23:15
  • Is `path` a file? Or is it perhaps a directory? What does `os.path.isdir(path)` yield? – David Heffernan Nov 09 '12 at 23:16
  • @DavidHeffernan: Opening a directory for write access raises Errno 21 on both Mac and Windows. – abarnert Nov 09 '12 at 23:17
  • @abarnert Well, it was a nice try! – David Heffernan Nov 09 '12 at 23:19
  • the path is a valid file! ...is there a programmatic way to check if that file is used by someone? (just for testing :P) – daveoncode Nov 09 '12 at 23:19
  • Just for testing, download the `Handle` tool I mentioned above; you can always script it via `subprocess`. If this isn't good enough for "real" code, you can find source code for some of the tools, and blogs explaining how Russinovich wrote them for others (although it was much nicer before Microsoft tool over Sysinternals, so no promises you'll be able to find enough info on any particular tool anymore), so you can, e.g., port the same thing to Python with PyWin32. Or there may be useful modules on PyPI or recipes at ActiveState. – abarnert Nov 09 '12 at 23:29
  • Actually, if you just want to check if it's used by _someone_, without knowing who, I think you can just call the Win32 function `CreateFile` (via PyWin32 again, or ctypes if you prefer) then `GetLastError`, and you get different Win32 error codes for "already open with exclusive access" vs. "no permissions"… but I'm not positive about that. – abarnert Nov 09 '12 at 23:32
  • Meanwhile, I believe you can test non-programmatically just by dragging the file from Explorer to Notepad, which will either open the file (which may take a while and look like a mess of garbage if it's, say, a 60MB binary executable…) or pop up a hopefully-useful error message. It's worth trying this first to rule out various possibilities. – abarnert Nov 09 '12 at 23:57
  • I did that test, thanks... read my updated question :/ – daveoncode Nov 10 '12 at 15:20
  • I'm not having this same problem, but getting false positives for os.path.exists in a script running in Python 3.3.0x86_64 under native Windows 7. I'm trying to check if a directory doesn't exist (not os.path.exists(path)) and it says it does, when it really doesn't. If I open an interactive shell, it reports correctly... – dciliske Dec 07 '12 at 01:50

0 Answers0