如何将FTP个文件放入某个地图

How to put FTP file into a certain map

remotefile=f'{fileName}'
with open(remotefile, "rb") as file:
    ftp.storbinary('STOR %s' % remotefile, file)

如何将文件放入某个地图?

如果你想输入 folder/directory 那么你可以做以下两件事之一

  1. STOR
  2. 之前更改远程文件夹
localfile  = 'test/input.txt'

remotefile = 'output.txt'

ftp.cwd('folder1/folder2')  # go to some folder

with open(localfile, 'rb') as file_handler:
    ftp.storbinary(f'STOR {remotefile}', file_handler)
  1. remotefile
  2. 中使用远程文件夹
localfile  = 'test/input.txt'

remotefile = 'folder1/folder2/output.txt'

with open(localfile, 'rb') as file_handler:
    ftp.storbinary(f'STOR {remotefile}', file_handler)

在这两种情况下,文件夹都必须存在。如果它不存在,那么您必须创建它。

首先你必须创建 folder1 然后 folder2,等等

ftp.mkd('folder1')
ftp.mkd('folder1/folder2')

编辑:

我用于测试的完整代码

import ftplib

ftp = ftplib.FTP()
ftp.connect('0.0.0.0', 2121)  # pyftpdlib as default runs on port 2121
ftp.login()

# --- test 0 (create nested folders) ---

ftp.mkd('folder1')
ftp.mkd('folder1/folder2')

# --- test 1 ---

localfile  = 'test/input.txt'
remotefile = 'folder1/folder2/output1.txt'

with open(localfile, 'rb') as file_handler:
    ftp.storbinary(f'STOR {remotefile}', file_handler)

# --- test 2 ---

localfile  = 'test/input.txt'
remotefile = 'output2.txt'

ftp.cwd('folder1/folder2')

with open(localfile, 'rb') as file_handler:
    ftp.storbinary(f'STOR {remotefile}', file_handler)

我使用 python 模块 pyftpdlib

在本地 FTP 服务器上测试了它
# install module

python -m pip install pyftpdlib

# run FTP server (with "write mode" for anonymous users)

python -m pyftpdlib --write

此服务器在端口 2121 而不是标准端口 21

上运行