首页 > 解决方案 > 使用 Python ftplib 上传文件时出现“不允许操作”或“不是常规文件”

问题描述

我想使用 Python 将文件上传到租用的 FTP 服务器(coreserver)。代码是这样的

# -*- coding: utf-8 -*-
import ftplib

def ftp_upload(hostname, username, password, upload_src_path, upload_dst_path):
    ftp = ftplib.FTP(hostname)
    ftp.set_pasv("true")
    ftp.login(username, password)
    fp = open(upload_src_path, 'rb')
    ftp.storbinary(upload_dst_path ,fp)

    ftp.close()
    fp.close()

hostname = "example.com"
upload_src_path = "/content/drive/My Drive/DSC_0569.JPG"
upload_dst_path = "STOR /public_html/example.com/"
username = "example"
password = "example"

ftp_upload(hostname, username, password, upload_src_path, upload_dst_path)

但错误是这样发生的

error_perm: 550 /public_html/example.com/: Operation not permitted

我试过了

STOR /public_html/
STOR /public_html
STOR /public_html/example.com/
STOR /public_html/example.com
STOR /example.com/
STOR /example.com

但错误总是发生

error_perm: 550 /public_html/example.com/: Operation not permitted

或者

error_perm: 550 /public_html/example.com/: Not a regular file

有人可以帮我解决这个问题吗?

标签: pythonftpftplib

解决方案


参数中的路径FTP.storbinary是目标文件的路径,而不仅仅是目标文件夹的路径。ftplib 怎么知道你上传的文件的名称?

所以应该是:

upload_dst_path = "STOR /public_html/xxxxxxx.com/DSC_0569.JPG"

推荐阅读