python gets the file from ftp and downloads it locally
- 2021-08-17 00:18:23
Recently, there is a demand that the pictures on the ftp address provided by the other party need to be obtained to the local server. Originally, it was planned to use shell operation, because shell itself also supports ftp commands, which can also meet the demand through for loop. But then I thought about taking python operation; So Baidu was carried out online; No exception is still so disappointed that it can't be copied directly. Therefore, it is modified on 1 code. There is still something to learn; The specific operation code is as follows. You can use it only by modifying the ftp account password to correspond to the directory
One point to note here is the usage of os. path. join
#!/usr/bin/python # -*- coding: utf-8 -*- """ FTP Common operations """ from ftplib import FTP import os class FTP_OP(object): def __init__(self, host, username, password, port): """ Initialization ftp :param host: ftp Host ip :param username: ftp User name :param password: ftp Password :param port: ftp Port (Default 21 ) """ self.host = host self.username = username self.password = password self.port = port def ftp_connect(self): """ Connect ftp :return: """ ftp = FTP() ftp.set_debuglevel(1) # Do not turn on debug mode ftp.connect(host=self.host, port=self.port) # Connect ftp ftp.login(self.username, self.password) # Login ftp ftp.set_pasv(False)##ftp Be proactive Passive mode Need to be adjusted return ftp def download_file(self, ftp_file_path, dst_file_path): """ From ftp Download files to local :param ftp_file_path: ftp Download file path :param dst_file_path: Local storage path :return: """ buffer_size = 102400 # The default is 8192 ftp = self.ftp_connect() print(ftp.getwelcome() ) # Show login ftp Information file_list = ftp.nlst(ftp_file_path) for file_name in file_list: print("file_name"+file_name) ftp_file = os.path.join(ftp_file_path, file_name) print("ftp_file:"+ftp_file) #write_file = os.path.join(dst_file_path, file_name) write_file = dst_file_path+file_name ## Here if you use os.path.join If splicing is carried out, Will be lost dst_file_path Path , Not with the above splicing path 1 Sample print("write_file"+write_file) if file_name.find('.png')>-1 and not os.path.exists(write_file): print("file_name:"+file_name) #ftp_file = os.path.join(ftp_file_path, file_name) #write_file = os.path.join(dst_file_path, file_name) with open(write_file, "wb") as f: ftp.retrbinary('RETR %s' % ftp_file, f.write, buffer_size) #f.close() ftp.quit() if __name__ == '__main__': host = "192.168.110.**" username = "****" password = "****" port = 21 ftp_file_path = "/erp-mall/" #FTP Directory dst_file_path = "/root/11" # Local directory ftp = FTP_OP(host=host, username=username, password=password, port=port) ftp.download_file(ftp_file_path=ftp_file_path, dst_file_path=dst_file_path)
The above is python from ftp to get the file and download to the local details, more about python ftp download file information please pay attention to other related articles on this site!