使用 Python ftplib 上传文件时出现“553 无法打开该文件:没有这样的文件或目录”

"553 Can't open that file: No such file or directory" when uploading a file using Python ftplib

得到这个 FTP 上传代码。此示例具有正确的数据,因此您可以尝试一下。 路径是正确的,因为我是从 select 框中获取的 (filename = askopenfilename()) 我得到:

ftplib.error_perm: 553 Can't open that file: No such file or directory

我在这里遇到错误:

cmd 'STOR /Users/rs/Desktop/DESKTOP/fields.png'
put 'STOR /Users/rs/Desktop/DESKTOP/fields.png\r\n'

import ftplib
from tkinter import Tk
from tkinter.filedialog import askopenfilename
import urllib


Tk().withdraw()
filename = askopenfilename()
print(filename)

from ftplib import FTP
import os
import fileinput

ftp = FTP()
ftp.set_debuglevel(2)
ftp.connect('ftpupload.net', 21)
ftp.login('ezyro_28887012', 'ricardo123456')
ftp.encoding = "utf-8"


print("List Dir: ",ftp.dir())

print("filename: ",filename)
with open(filename, "rb") as file:
    ftp.storbinary(f"STOR {filename}", file)

# Get list of files
print("NEW DIR AFTER UPLOAD: ",ftp.dir())

# Close the Connection
ftp.quit()

有人可以帮忙吗?

/usr/local/bin/python3.9 /Users/rs/PycharmProjects/OCPP/ftpconf.py
/Users/rs/Desktop/DESKTOP/issue fields.png
*get* '220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------\n'
*get* '220-You are user number 292 of 6900 allowed.\n'
*get* '220-Local time is now 08:51. Server port: 21.\n'
*get* '220-This is a private system - No anonymous login\n'
*get* '220 You will be disconnected after 60 seconds of inactivity.\n'
*resp* '220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------\n220-You are user number 292 of 6900 allowed.\n220-Local time is now 08:51. Server port: 21.\n220-This is a private system - No anonymous login\n220 You will be disconnected after 60 seconds of inactivity.'
*cmd* 'USER ezyro_28887012'
*put* 'USER ezyro_28887012\r\n'
*get* '331 User ezyro_28887012 OK. Password required\n'
*resp* '331 User ezyro_28887012 OK. Password required'
*cmd* 'PASS *************'
*put* 'PASS *************\r\n'
*get* '230-Your bandwidth usage is restricted\n'
*get* '230 OK. Current restricted directory is /\n'
*resp* '230-Your bandwidth usage is restricted\n230 OK. Current restricted directory is /'
*cmd* 'TYPE A'
*put* 'TYPE A\r\n'
*get* '200 TYPE is now ASCII\n'
*resp* '200 TYPE is now ASCII'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '227 Entering Passive Mode (185,27,134,11,171,227)\n'
*resp* '227 Entering Passive Mode (185,27,134,11,171,227)'
*cmd* 'LIST htdocs'
*put* 'LIST htdocs\r\n'
*get* '150 Accepted data connection\n'
*resp* '150 Accepted data connection'
drwxr-xr-x    2 28887012   28887012          108 Aug 13 08:47 .
drwxr-xr-x    3 0          0                 106 Jun 15 05:39 ..
-rw-r--r--    1 28887012   28887012        49619 Jul 27 10:32 210727_logs_16040279_10566_3.4.2.zip
-rw-r--r--    1 28887012   28887012      8695466 Jul 27 06:17 ECP_ISO_3.4.2.7z
-rw-r--r--    1 28887012   28887012          229 Jun 30 07:46 index.html
*get* '226-Options: -a -l \n'
*get* '226 5 matches total\n'
*resp* '226-Options: -a -l \n226 5 matches total'
*cmd* 'TYPE A'
*put* 'TYPE A\r\n'
*get* '200 TYPE is now ASCII\n'
*resp* '200 TYPE is now ASCII'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '227 Entering Passive Mode (185,27,134,11,112,120)\n'
*resp* '227 Entering Passive Mode (185,27,134,11,112,120)'
*cmd* 'LIST'
*put* 'LIST\r\n'
*get* '150 Accepted data connection\n'
*resp* '150 Accepted data connection'
drwxr-xr-x    3 0          0                 106 Jun 15 05:39 .
drwxr-xr-x    3 0          0                 106 Jun 15 05:39 ..
-r--r--r--    1 0          0                  90 Jun 15 05:39 .htaccess
-rw-r--r--    1 0          2                   0 Jun 15 05:39 .override
-rw-r--r--    1 0          2                   0 Jun 15 05:39 DO NOT UPLOAD FILES HERE
drwxr-xr-x    2 28887012   28887012          108 Aug 13 08:47 htdocs
*get* '226-Options: -a -l \n'
*get* '226 6 matches total\n'
*resp* '226-Options: -a -l \n226 6 matches total'
List Dir:  None
--------> <_io.BufferedReader name='/Users/rs/Desktop/DESKTOP/issue fields.png'>
-aaaaaaaaaaaaa---> /Users/rs/Desktop/DESKTOP/issue fields.png
*cmd* 'TYPE I'
*put* 'TYPE I\r\n'
*get* '200 TYPE is now 8-bit binary\n'
*resp* '200 TYPE is now 8-bit binary'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '227 Entering Passive Mode (185,27,134,11,228,17)\n'
*resp* '227 Entering Passive Mode (185,27,134,11,228,17)'
*cmd* 'STOR /Users/rs/Desktop/DESKTOP/issue fields.png'
*put* 'STOR /Users/rs/Desktop/DESKTOP/issue fields.png\r\n'
Traceback (most recent call last):
  File "/Users/rs/PycharmProjects/OCPP/ftpconf.py", line 32, in <module>
    ftp.storbinary(f"STOR {filename}", file)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ftplib.py", line 498, in storbinary
    with self.transfercmd(cmd, rest) as conn:
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ftplib.py", line 393, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ftplib.py", line 359, in ntransfercmd
    resp = self.sendcmd(cmd)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ftplib.py", line 281, in sendcmd
    return self.getresp()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ftplib.py", line 254, in getresp
    raise error_perm(resp)
ftplib.error_perm: 553 Can't open that file: No such file or directory
*get* "553 Can't open that file: No such file or directory\n"
*resp* "553 Can't open that file: No such file or directory"

Process finished with exit code 1

您正在尝试使用本地路径将文件保存在远程服务器上。请注意您如何使用与 local openremote storbinary 相同的路径。我很确定您的远程服务器具有不同的目录结构。

如果您想将文件上传到您的 FTP 主目录,只需使用文件名:

ftp.storbinary("STOR " + os.path.basename(filename), file)

如果您想上传到特定文件夹,请使用 FTP 服务器上的真实路径,例如:

ftp.storbinary("STOR htdocs/" + os.path.basename(filename), file)