Using python to Realize the File Reading and Writing Method of ftp
- 2021-07-09 08:28:55
- OfStack
ftp login connection
from ftplib import FTP # Loading ftp Module
ftp=FTP() # Set variables
ftp.set_debuglevel(2) # Turn on the debug level 2 Display details
ftp.connect("IP","port") # Connected ftp sever And ports
ftp.login("user","password") # User name and password for connection
print ftp.getwelcome() # Print out the welcome message
ftp.cmd("xxx/xxx") # Enter the remote directory
bufsize=1024 # Set buffer size
filename="filename.txt" # Files to be downloaded
file_handle=open(filename,"wb").write # Open a file locally in write mode
ftp.retrbinaly("RETR filename.txt",file_handle,bufsize) # Receive files on the server and write to local files
ftp.set_debuglevel(0) # Turn off debug mode
ftp.quit() # Quit ftp
ftp Related command operation
ftp.cwd(pathname) # Settings FTP The path of the current operation
ftp.dir() # Display all directory information under the directory
ftp.nlst() # Get the files in the directory
ftp.mkd(pathname) # New Remote Directory
ftp.pwd() # Return to the current location
ftp.rmd(dirname) # Delete Remote Directory
ftp.delete(filename) # Delete remote files
ftp.rename(fromname, toname)# Will fromname Modify the name to toname .
ftp.storbinaly("STOR filename.txt",file_handel,bufsize) # Upload target file
ftp.retrbinary("RETR filename.txt",file_handel,bufsize) # Download FTP Documents