python错误错误550:没有这样的文件,但只有绝对路径

2024-04-29 10:29:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一点python代码来上传一些gifs到FTP服务器。当我的脚本在我的gif dir中时,当我使用img = "name.gif"之类的东西时,代码工作得很好。在

但只要我想使用下面这样的绝对路径:

img = "/home/pi/photobooth/gif/name.gif"

我收到一个550错误提示我/home/pi/photobooth/gif/name.gif: no such file or directory

有人知道为什么吗?

我的代码如下:

from ftplib import FTP
import sys

def ULimg(img):
    host = 'host.com'
    user = 'user'
    passwd = 'pass'

    try:
        ftp = FTP(host)
        ftp.set_pasv(False)
        ftp.login(user, passwd)
    except:
        print('Error connecting to FTP server')
        sys.exit()

    file = open(img, 'rb')
    ftp.storbinary('STOR ' + img, file)
    file.close()
    ftp.quit()

img = '/home/pi/photobooth/gif/name.gif'
ULimg(img)

下面是我使用绝对路径时得到的error

回溯(最近一次呼叫):

^{pr2}$

如果有人能帮忙,谢谢!


Tags: 代码nameimporthosthomeimgsyspi