![]() ftplib.error_proto – malformed server reply.ftplib.error_reply – unexpected server reply.It may be useful to know when an FTP command has not been executed successfully, for this you must take into account that the code can throw one of the following errors: With open('local_image.png', 'wb') as local_file:įtp.retrbinary('RETR image.png', local_file.write)įinally, delete a file, using the delete(”file_name_delete.txt”) function : from ftplib import FTP, all_errors If response.startswith('226'): # Full transfer Response = ftp.retrlines('RETR text.txt', local_file.write) With open('local_text_name.txt', 'w') as local_file: # Open a text file locally for writing To do this you will use the retrlines and retrbinary functions, for text and binary files respectively: from ftplib import FTP, all_errors Print(f"Error getting image size: ')Īnd not least, one of the functions that you will surely use the most is downloading files. Print(ftp.size('image.png')) # get the size of the image on the server ![]() To obtain the size of a file or archive, use the size() function, but you must take into account the type of file from which you are obtaining this information, so you must prepare the client to receive information from text files with the value «TYPE A» or for binary files with «TYPE I». With open('image.png', 'rb') as image_file:įtp.storbinary('STOR image.png', image_file) With open('file.txt', 'rb') as text_file:įtp.storlines('STOR file.txt', text_file) # For text or binary file, always use `rb` There are two ways to send files depending on whether they are text or binary files such as images, for example.įor text you will use the storlines function and for binary files storbinary: from ftplib import FTP You can list the contents of a directory by first getting an array of files with ftp.dir(files.append) : from ftplib import FTP To change the active directory you are in you must use ftp.cwd(‘another_directory’) : from ftplib import FTP You can remove the directory with the ftp.rmd(‘my_directory’) function using the directory name as an argument: from ftplib import FTP You can create a directory or folder using the ftp.mkd(‘my_directory’) function and passing the directory name as an argument: from ftplib import FTP With the function ftp.pwd() you can show the directory you are in: from ftplib import FTP Example of FTP Client in Python in console
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |