python - IO错误 : [Errno 2] No such file or directory (when it really exist) Python

标签 python file-transfer uart errno

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

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)

关于python - IO错误 : [Errno 2] No such file or directory (when it really exist) Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45853881/

相关文章:

python - 如何修改 QDataWidgetMapper 的值

python - 如何从左到右提取数字中的数字?

c - 通过套接字传输文件,最终大小字节数较少

architecture - 什么时候使用 FTP 比较好?

c - 中断发送和接收字符串

python - 为 KeyError 打印出奇怪的错误消息

http - 为(可能)非常大的文本文件发送部分更新/添加

c - 在STM32F407中断处理程序中禁用中断

flutter - 通过 USB 端口进行串行交互以在 Flutter 上开发 Windows 应用程序

python - 如何通过 Doc2Vec 找到文档中最关键的句子或单词?