Python Forum
A working code to upload a file using sftp?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A working code to upload a file using sftp?
#2
Well, I found too many codes for the same thing, can you try the below code, I hope you can get an idea of what you are looking for.

Quote:from ftplib import FTP

# FTP server details
ftp_host = 'ftp.example.com' # Replace with your FTP server address
ftp_user = 'your_username' # Replace with your FTP username
ftp_pass = 'your_password' # Replace with your FTP password

# Local file to upload
local_file_path = 'local_file.txt' # Replace with the path to your local file

# Remote directory where you want to upload the file
remote_directory = '/public_html/' # Replace with the desired remote directory

try:
# Connect to the FTP server
ftp = FTP(ftp_host)
ftp.login(user=ftp_user, passwd=ftp_pass)

# Change to the target directory on the server
ftp.cwd(remote_directory)

# Open the local file in binary mode for uploading
with open(local_file_path, 'rb') as local_file:
# Upload the file to the FTP server
ftp.storbinary('STOR ' + local_file_path, local_file)

print(f"File '{local_file_path}' uploaded successfully to '{remote_directory}'")

# Close the FTP connection
ftp.quit()

except Exception as e:
print(f"An error occurred: {e}")

Thanks
Reply


Messages In This Thread
RE: A working code to upload a file using sftp? - by gulshan212 - Nov-06-2023, 04:42 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020