I am using paramiko to upload files to SFTP server. I want to transfer all files in folder. The names of files are in a manner one.txt
, two.txt
.... . I want the files to be sent sequentially like one.txt
then two.txt
then three.txt
.....the below code for transfer of one file runs fine, but the last code which I have tried to transfer all files is not working....
import paramiko
source = r'/home/netcs/b/one.txt'
dest = r'/home/tein/c/pickle.txt'
hostname = '10.10.10.9'
port = 22 # default port for SSH
username = 'tein'
password = 'po'
try:
t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(source, dest)
finally:
t.close()
Transfer all files (not working):
import paramiko
source = r'/home/netcs/b/'
dest = r'/home/tein/c/'
hostname = '10.10.10.9'
port = 22 # default port for SSH
username = 'tein'
password = 'po'
for file in source:
if file.endswith('.txt'):
try:
t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(source, dest)
finally:
t.close()
break
else:
print('No txt file found')
But the script's output is:
no txt file found
no txt file found
no txt file found