How should I make it so that the file I download from ftp is recorded in a specific directory, and not where the artist file is stored?

import ftplib host = "*" ftp_user = "*" ftp_password = "*" filename = "picture.png" con = ftplib.FTP(host, ftp_user, ftp_password) lf = open(filename, "wb") con.retrbinary("RETR " + filename, lf.write) lf.close() 

saving the file to another folder is wrong C:\Users\\Desktop\try\d and /d

  • Add the path to the filename variable, something like filename = "dir/nextdir/picture.png" so the file is saved to the dir/nextdir/ directory - Igor Igoryanych
  • @ Igor igoryanych unless this is the name of the file on ftp? - Twiss
  • @IhorIgoryanych I need not on ftp but from ftp to a specific directory on the disk - Twiss

1 answer 1

Your filename is both a remote and local name. To change the local path only to download the file to another directory, just explicitly specify the path:

 import os with ftplib.FTP(host, *credentials) as ftp, \ open(os.path.join("директория", filename), "wb") as file: ftp.retrbinary("RETR " + filename, file.write) 
  • But you can write something like not a complete path, but by type in the subfolder of the program's root folder edited the question - Twiss
  • Yes, and in fact, the moderator deleted my similar answer by writing "Is it somehow connected with ftp?" - Twiss
  • one
    @ Dmitry can. Instead of the string "директория" you can specify the full path and relative to the current directory. See Current directory in Python . Your question as a matter of fact does not include ftp: "How to create a file in the specified folder" - jfs