Open failure, cryptic debugger output

I’m having trouble with the following simple open statement. The code dies on that
line and the diagnostics are of no help.` This is Python 3.9 PyCharm

import pdb
OBPRINT = open(“c:\users\asha\ilsmom3a\”, “w”)

The code dies on the OBPRINT line. Here’s the output in the debugger window

C:\Users\asha\PycharmProjects\pythonProject1\venv\Scripts\python.exe -m pdb “C:\Users\asha\AppData\Local\JetBrains\PyCharm Community Edition 2020.3.5\plugins\python-ce\helpers\pydev\” --multiproc --qt-support=auto --client --port 60349 --file c:/users/asha/

c:\users\asha\appdata\local\jetbrains\pycharm community edition 2020.3.5\plugins\python-ce\helpers\pydev\
→ ‘’’
Process finished with exit code -1

What’s going on?


It might be the case that you have copied the code from somewhere, and the double inverted commas are not compatible. So, there are two potential fixes:

First, always use a raw string for addresses. So just put a r before the address string in open statement.

Second, retype the double inverted commas.

What exception do you get if you take the debugger out of the equation
and just run the open function?

Hi Sanket,

If the quotes were Unicode “smart quotes” or “double inverted commas”,
the code wouldn’t even get to the debugger because it would be an
immediate syntax error:

>>> open(“filename”)
  File "<stdin>", line 1
SyntaxError: invalid character '“' (U+201C)

Also, it is not good advice to use raw strings for file names. I know
lots of people do it on Windows, but it is still not the best advice.
The problem is that raw strings cannot end with a backslash, so you
cannot write a path like this:

path = r'path\to\directory\'

The generally preferred way to write pathnames on Windows is to use
forward slashes:

path = 'path/to/directory/'

The Windows OS will accept forward slashes instead of backslashes for
paths under most circumstances. In the context of using Python’s open()
function, it will always work.

1 Like

@steven.daprano thanks for the advice. I didn’t yet knew raw strings are not 100% raw, lol!

Thanks for the replies. I don’t know what went wrong. All I did was delete the offending statement and retype it from scratch and then it worked fine.