Francis Michels
Francis Michels

Reputation: 117

Download from FTP with Python - Pathname

I'm using the ftplib to connect to my FTP-server.

I want to download a file from my FTP-server to a specified directory on my computer. I have this simple code:

def download(ftp,file):
    f = open(file,"wb")
    ftp.retrbinary("RETR " + file,f.write)
    f.close()

What do I have to add to this code to download the file to my requested directory?

Thanks!

Upvotes: 0

Views: 375

Answers (1)

J.J.
J.J.

Reputation: 5069

Update the call to open with the local path you want to write to. For example:

import os
def download(ftp,file, localdir):
    f = open(os.path.join(localdir, file),"wb")
    ftp.retrbinary("RETR " + file,f.write)
    f.close()

Upvotes: 1

Related Questions