Python无法识别路径

时间:2018-08-01 07:51:07

标签: python

def whatNumIsThis(filePath):
    matchedAr = []
    loadExamps = open("C:/Users/p.de.falco/Desktop/images/numArEx.txt", "r").read()
    loadExamps = loadExamps.split("\n")

    i = Image.open(filePath)
    iar = np.array(i)
    iarl = iar.tolist()

    inQuestion = str(iarl)
    for eachExample in loadExamps:
        if len(eachExample) > 3:
            splitEx = eachExample.split("::")
            currentNum = splitEx[0]
            currentAr = splitEx[1]

            eachPixEx = currentAr.split("],")
            eachPixInQ = inQuestion.split("],")
            x = 0
            while x < len(eachPixEx):
                if eachPixEx[x] == eachPixInQ[x]:
                    matchedAr.append(int(currentNum))
                x += 1

    print(matchedAr)
    x = Counter(matchedAr)
    print(x)


whatNumIsThis("C:/Users/p.de.falco/Desktop/images/test.png")

嗨,这是更大代码的一部分。它给了我这个错误:

Traceback (most recent call last):
  File "C:/Users/p.de.falco/PycharmProjects/test_1/detect3.py", line 30, in <module>
    whatNumIsThis('C:/Users/p.de.falco/Desktop/images/test.png')
  File "C:/Users/p.de.falco/PycharmProjects/test_1/detect3.py", line 3, in whatNumIsThis
    loadExamps = open('C:/Users/p.de.falco/Desktop/images/numArEx.txt', 'r').read()
IOError: [Errno 2] No such file or directory: 'C:/Users/p.de.falco/Desktop/images/numArEx.txt'

我将路径与我在代码中放置的其他路径相同,但是在这种情况下,它无法找到文件。我正在使用pycharm和anaconda3,有人知道如何解决此问题吗?谢谢你!

1 个答案:

答案 0 :(得分:1)

看起来您的文件在给定路径上不存在
您可以通过以下方式在打开文件之前验证其中一个文件是否存在:

import os.path
os.path.exists('C:/Users/p.de.falco/Desktop/images/numArEx.txt')

如果文件存在则返回True
希望这可以帮助!
干杯!