简体   繁体   中英

sftp file upload with python - avoiding file renaming

I want to create a python script that enables me to upload files in a specific folder via sftp. The directories are allways the same. This is what I came up with:

import pysftp
import os
import shutil

cnopts = pysftp.CnOpts(knownhosts='~/.ssh/authorized_keys')
cnopts.hostkeys = None


host = "xxx"
password = "xxx"
username = "xxx"

srv = pysftp.Connection(host, username=username, password=password, cnopts=cnopts)

# src_files = os.listdir("C:/Users/uploads")
remotepath = "/home/work"
localpath = "C:/Users/uploads"
# with srv.cd('/home/work'):  # chdir to work
with srv.cd():
    srv.chdir('schedule')
    srv.put_d(localpath, remotepath)

print('Upload done')
srv.close()

Whats happening is that all files get transfered to the folder /home/work/schedule however they are named like this "work.\\file name". But I want the file names to be identical. I tried to exclude the remotepath replacing it with '' but still the files are being renamed to '\\.\\file name' What is the problem here?

As you can see I tried to work with os and shutil but that didnt work out for me. So its still in the code but not necessary.

Thank you for your input

have you try:

with srv.cd(remotepath):
    srv.put_d(localpath, 'schedule')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM