1

I need to remove each image after uploading it to my server I use paramiko to upload images:

remote_dir = "/path/on/remote/server/"
with sftp.Server("user", "pass", "www.example.com") as server:
for image in glob.glob("/local/path/to/*.png"):
    base = path.basename(image)
    server.upload(image, path.join(remote_dir, base))
    os.remove(image)

However, each image is removed, before upload function finishes its work.

How can I run os.remove(image) after server.upload uploads an image to the server successfully? I am new to python and heard about python-callback, but don't know how to use it in this situation.

korujzade
  • 400
  • 4
  • 23
  • what python web framework do you use? – fodma1 Dec 25 '15 at 10:43
  • @fodma1 I am not using any web framework. it is paramiko for uploading files to server through sftp – korujzade Dec 25 '15 at 10:43
  • What kind of error do you get? Could you include the way you create the server variable? – fodma1 Dec 25 '15 at 10:48
  • @fodma1 I don't get any error, but when I check the server for images, I see that they are not properly uploaded ("failed to load image" error occurs when I try to open the images). Removing `os.remove(image)` part from the code solves the problem. – korujzade Dec 25 '15 at 10:57
  • @fodma1 I follows this tutorial to connect to the server and upload files. http://ginstrom.com/scribbles/2009/09/14/easy-sftp-uploading-with-paramiko/ – korujzade Dec 25 '15 at 11:01
  • Not that the resource you referred is almost 7 years old. I was checking the documentation, and I couldn't find this interface: http://docs.paramiko.org/en/1.16/ I guess this answer might help you: http://stackoverflow.com/a/3635163/2419215 As for the callback it's different from javascript async callbacks, in Python everything should be synchronous. You can pass a callback function to the put method, but it's just for displaying the status of the upload: http://docs.paramiko.org/en/1.16/api/sftp.html#paramiko.sftp_client.SFTPClient.put – fodma1 Dec 25 '15 at 11:12

0 Answers0