libftp:从脚本以外的路径存储文件



我在本地家用电脑上运行了一台FTP服务器,然后我有一台Python脚本所在的远程服务器。

脚本位于该路径中(由于其他原因必须保留在该路径中(:/home/skizzo/files/Upload/Games

我想从远程服务器传输到本地家用电脑的文件位于以下路径:/home/skizzo/files/Upload/Games/torrents

我看到ftp.cwd()命令用于更改目标文件夹,然后是我要保存文件的文件夹,然后更改本地家用电脑的文件夹。相反,我想更改远程服务器的文件夹,从哪里获取要传输的文件。

我该怎么办?这是我的脚本:

torrent_name = 'file.torrent'
def ftp(torrent_name):
FTP_HOST = "ftp.example.net"
FTP_USER = "username"
FTP_PASS = "123abc"
# connect to the FTP server
ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
# force UTF-8 encoding
ftp.encoding = "utf-8"
# file name you want to upload
with open(torrent_name, "rb") as file:
# use FTP's STOR command to upload the file
ftp.storbinary(f"STOR {torrent_name}", file)
ftp.quit()

您只需在打开文件之前添加路径:

torrent_name = 'file.torrent'
torrent_dir = '/home/skizzo/files/Upload/Games/torrents'
def ftp(torrent_name, torrent_dir):
...
# file name you want to upload
with open(os.path.join(torrent_dir, torrent_name), "rb") as file:
...

最新更新