Home > Blockchain >  libftp: store a file from a path other than that of the script
libftp: store a file from a path other than that of the script

Time:01-19

I have an FTP server running on my local home PC and then I have a remote server where the Python script resides.

The script is in this path (and must remain there for other reasons): /home/skizzo/files/Upload/Games

The file I want to transfer from the remote server to the local home PC is instead in this path: /home/skizzo/files/Upload/Games/torrents

I saw that the ftp.cwd() command is used to change the destination folder, then the one where I'm going to save the file, then that of the local home PC. Instead, I want to change that of the remote server, where to go to fetch the file to be transferred.

How can I do? This is my script:

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()

CodePudding user response:

You could simply add the path before opening the file:

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:
        ...
  •  Tags:  
  • Related