Transfer and manipulate your files and folders using our FTP and SFTP.
pip install botcity-ftp-plugin
Instantiating the plugin¶
First, let's instantiate the plugin passing the information that we will use to connect to the ftp server.
hostname = "<SERVER_NAME>" username = "<USER_LOGIN>" password = "<USER_PASSWORD>" # Instantiate the plugin ftp = BotFTPPlugin(hostname, username, password)
If no username and password are passed, the login attempt to the server will be done using an anonymous user by default. Some servers do not allow anonymous login, so pay attention to this.
With this plugin we can do the most basic operations with files and easily manage local files and files that are on the server.
Before performing file operations, make sure you are in the correct directory.
set_current_directory method to set your current working directory on the server.
Uploading and downloading¶
# Connecting to server ftp = BotFTPPlugin("<FTP_SERVER>", "<USER>", "<PASSWORD>") # Uploading a local file named example.txt to the server ftp.upload_file('my_files/example.txt') # Downloading a file named README stored on the server and saving in the local path: ftp_files/Downloads ftp.download_file('README', 'ftp_files/Downloads') # Closes the connection with the server ftp.disconnect()
Renaming and deleting¶
# Renaming the file named "my_file.txt" stored on the server. ftp.rename_file('my_file.txt', 'my_updated_file.txt') # Deleting a file from the server by name ftp.delete_file('my_updated_file.txt')
When renaming a file you can also pass a full path instead of just the file name. In this case, the file in addition to being renamed will be moved to the path that was passed.
It is also possible to do basic operations with the directories present on the server.
ftp = BotFTPPlugin("<FTP_SERVER>", "<USER>", "<PASSWORD>") # Prints the path of the current working directory. current_folder = ftp.get_current_directory() print(current_folder) # Prints the list of all files and folders contained in the current working directory ftp.list_files() # Remove the specified directory from the server ftp.remove_directory('tests/files/directory_1') # Create a new directory on the server. The full path of the created directory will be returned. dir_path = ftp.create_directory('my_directory') # Changes the current working directory to the new created directory ftp.set_current_directory(dir_path)
Let's take a look into the complete code:
from botcity.plugins.ftp import BotFTPPlugin # Connecting to server ftp = BotFTPPlugin("<FTP_SERVER>", "<USER>", "<PASSWORD>") # Creating a new directory and setting it to the current working directory dir_path = ftp.create_directory('tests/my_directory') ftp.set_current_directory(dir_path) # For each file in the local folder, upload it to the folder created on the server local_folder = 'downloads/files' for file in os.listdir(local_folder): ftp.upload_file(os.path.join(local_folder, file)) ftp.list_files() # Closes the connection with the server ftp.disconnect()
Working with SFTP servers¶
In addition to managing files on an FTP server, with this plugin it is also possible to perform the same operations shown above on SFTP servers through an SSH connection.
To use these same operations on an SFTP server, just instantiate an object of the
from botcity.plugins.ftp import BotSFTPPlugin # Connecting to server sftp = BotSFTPPlugin("<SFTP_SERVER>", 22, "<USER>", "<PASSWORD>") # Prints the name of each file in the current working directory for file in sftp.get_files_list(): print(file) # Creating a new directory on the server sftp.create_directory("folders/my_folder") # Upload a file to the folder created on the server sftp.upload_file("image.png", "folders/my_folder") # Closes the connection with the server sftp.disconnect()