"No such file or directory" error with both file in same directory AND when using absolute path

时间:2017-08-05 11:26:12

标签: python

all. I'm running into a "no such file or directory" issue in python that's stumped me.

Things I've tried so far:

  • Closing any program that I could think might have the file open
  • Having the file in the same directory as the program I'm running
  • Using the absolute path name
  • Escaping the backslashes
  • Escaping the backslashes and spaces
  • Changing the backslashes to forward slashes
  • Removing all spaces, special symbols, and numbers from the filename

I even checked with os.getcwd and os.path.abspath and copy-pasted the path exactly.

I'm not sure what's going on here. I'm at a loss now. Would I get this same error if the file is still open in some elusive background program?

This is the relevant bit of code:

print(os.getcwd())
print(os.path.abspath('RainyGenki.json'))

deckName = "C:\Users\myName\My Documents\LiClipse Workspace\KanjiDrag\RainyGenki.json"
deck = open(deckName, 'r')   #opens card deck

This is the error message:

C:\Users\myName\My Documents\LiClipse Workspace\KanjiDrag
C:\Users\myName\My Documents\LiClipse Workspace\KanjiDrag\RainyGenki.json
Traceback (most recent call last):
  File "C:\Users\myName\My Documents\LiClipse Workspace\KanjiDrag\kanji_drag\kanji_main.py", line 79, in <module>
    deck = open(deckName, 'r')   #opens card deck
IOError: [Errno 2] No such file or directory: 'C:\\Users\\myName\\My Documents\\LiClipse Workspace\\KanjiDrag\\RainyGenki.json'

2 个答案:

答案 0 :(得分:0)

Using a raw string...

If you are sure that your path is OK then use the python syntax for a raw string:

In plain English: String literals can be enclosed in matching single quotes (') or double quotes ("). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings). The backslash () character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. String literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and use different rules for interpreting backslash escape sequences. A prefix of 'u' or 'U' makes the string a Unicode string. Unicode strings use the Unicode character set as defined by the Unicode Consortium and ISO 10646. Some additional escape sequences, described below, are available in Unicode strings. A prefix of 'b' or 'B' is ignored in Python 2; it indicates that the literal should become a bytes literal in Python 3 (e.g. when code is automatically converted with 2to3). A 'u' or 'b' prefix may be followed by an 'r' prefix.

This basically means that to escape the backslash escape sequences, you just need to put an 'r'before the string like:

deckName = r"C:\Users\myName\My Documents\LiClipse Workspace\KanjiDrag\RainyGenki.json"
ck = open(deckName, "r")

And even though you say you have tried it, escaping the backslashes should also work:

deckName = "C:\\Users\\myName\\My Documents\\LiClipse Workspace\\KanjiDrag\\RainyGenki.json"
ck = open(deckName, "r")

答案 1 :(得分:-1)

我有一个名为KanjiDrag的文件夹,在其中我有实际的源文件夹kanji_drag,这是json文件和主模块所在的位置。我使用的路径是访问KanjiDrag文件夹,但不是kanji_drag文件夹,我没有捕获不同的名称。这是我最愚蠢的文件IO错误。感谢所有的回复,当我改进我的程序的这一部分时,我还会提到很多回复。