How to upload file to FTP server in Python?

Asked

Viewed 250 times

0

I have a script that generates a CSV file and saves with the name of the current date. I’m trying to make a script that checks the latest created file and sends it to the FTP server.

How can I do that?

My script below:

import pysftp 
import datetime 
import os 
import glob

myHostname = 'sftp' 
myUsername = 'user' 
myPassword = 'password'

cnopts = pysftp.CnOpts() cnopts = pysftp.CnOpts(knownhosts=r'C:\Users\Thiago Simas.ssh\known_hosts')

sftp = pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts)

list_of_files = glob.glob(r'C:\Users\ThiagoSimas\PycharmProjects\pythonProject*.csv') # para puxar todos os arquivos csv 
latest_file = max(list_of_files, key=os.path.getctime) # para pegar o ultimo arquivo na pasta

localFilePath = latest_file 
remoteFilePath = '/Teste/

sftp.put(localFilePath, remoteFilePath)
sftp.close()
  • Missing close quotes in remoteFilePath.

  • Thanks for the help

1 answer

-2

Reference link here

import ftplib
ftp = ftplib.FTP("192.168.2.222")
ftp.login("test", "123qwe")
localfile='/tmp/test.txt'
remotefile='test.txt'
with open(localfile, "rb") as file:
    ftp.storbinary('STOR %s' % remotefile, file)

read file in ftp


import ftplib
ftp = ftplib.FTP("192.168.2.222")
ftp.login("test", "123qwe")
print("Directory: {}".format(ftp.pwd()))
ftp.retrlines("LIST")
ftp.quit()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.