请问这是为什么FileNotFoundError: [Errno 2] No such file or directory: 'img_path'

是我从网上看视频跟着人家一步一步敲的,不知道哪里出问题了,图片跟程序在一个目录下面。


import uuid
import requests
import base64
import hashlib
import time
import json

YOUDAO_URL = 'https://openapi.youdao.com/ocr_table'
APP_KEY = '75038d83358839f1'
APP_SECRET = 'XakTRoj1uHPMpltPiR04xw55z7eTrPkF'


def truncate(q):
    if q is None:
        return None
    q_utf8 = q.decode("utf-8")
    size = len(q_utf8)
    return q_utf8 if size <= 20 else q_utf8[0:10] + str(size) + q_utf8[size - 10:size]


def encrypt(signStr):
    hash_algorithm = hashlib.sha256()
    hash_algorithm.update(signStr.encode('utf-8'))
    return hash_algorithm.hexdigest()


def do_request(data):
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    return requests.post(YOUDAO_URL, data=data, headers=headers)


def connect(img_path):
    f = open(r'img_path', 'rb')  # 二进制方式打开图文件
    q = base64.b64encode(f.read())  # 读取文件内容,转换为base64编码
    f.close()

    data = {}
    data['type'] = '1'
    data['q'] = q
    data['docType'] = 'excel' # excel相关数据
    data['signType'] = 'v3'
    curtime = str(int(time.time()))
    data['curtime'] = curtime
    salt = str(uuid.uuid1())
    signStr = APP_KEY + truncate(q) + salt + curtime + APP_SECRET
    sign = encrypt(signStr)
    data['appKey'] = APP_KEY
    data['salt'] = salt
    data['sign'] = sign

    # 包含Excel相关数据(base64字符串)   json
    response = do_request(data)
    # json -- 字典
    # 拿到的是包含Excel的base64的json字符串
    response_json = json.loads(response.text)
    # 字典取值
    print(response_json)
    # 提取数据
    excel_table_base64 = response_json.get('Result').get('tables')[0]
    print('转换完成')
    return excel_table_base64

def orc_excel(img_path,excel_path):
    excel_table_base64 = connect(img_path)
    # 将数据解码
    decoded = base64.b64decode(excel_table_base64)

    # 将解码后的数据写入Excel文件
    with open(excel_path,'wb') as f:
        f.write(decoded)
        print('保存成功')


if __name__ == '__main__':
    img_path = r'./table.jpg'
    excel_path = r'./file.xlsx'
    # 获取数据并且转换后---保存数据
    orc_excel(img_path,excel_path)

img

这里应该是引用的变量img_path
把r’‘去掉

img

img

你把你个文件目录 图像识别 换成img_path试试

请看👉 :如何解决:FileNotFoundError: 以及[Errno 2]No such file or directory