IOError:[Errno 2]没有这样的文件或目录(当它确实存在时)Python [重复]

2023-12-21

我正在通过 python 中的 uart 传输文件文件夹。下面您可以看到简单的功能,但有一个问题,因为我收到如标题所示的错误:IOError: [Errno 2] No such file or directory: '1.jpg'其中 1.jpg 是测试文件夹中的文件之一。所以这很奇怪,因为程序知道它不存在的文件名?!我做错了什么?

def send2():
    path = '/home/pi/Downloads/test/'
    arr = os.listdir(path)
    for x in arr:
        with open(x, 'rb') as fh:
            while True:
                # send in 1024byte parts
                chunk = fh.read(1024)
                if not chunk: break
                ser.write(chunk)

如果要打开的文件不在您的工作目录中,您需要提供它们的实际完整路径:

import os
def send2():
    path = '/home/pi/Downloads/test/'
    arr = os.listdir(path)
    for x in arr:
        xpath = os.path.join(path,x)
        with open(xpath, 'rb') as fh:
            while True:
                # send in 1024byte parts
                chunk = fh.read(1024)
                if not chunk: break
                ser.write(chunk)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

IOError:[Errno 2]没有这样的文件或目录(当它确实存在时)Python [重复] 的相关文章

随机推荐