-2

I have to download some files from sftp using Python, I've tried to use listdir to list all of them, but my first attempt to use pysftp.listdir I receive this message : "module 'pysftp' has no attribute 'listdir'

import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None

myHostname = "99.99.999.999"
myUsername = "user"
myPassword = "*********"

with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword,cnopts=cnopts) as sftp:
    print("Connection succesfully stablished ... ")
    pysftp.cd('public')
    pysftp.listdir()

Gabio
  • 9,126
  • 3
  • 12
  • 32
user202135
  • 43
  • 8

2 Answers2

1

Change:

pysftp.cd('public')
pysftp.listdir()

To:

sftp.cd('public')
sftp.listdir()
Gabio
  • 9,126
  • 3
  • 12
  • 32
0

Since you are using the with keyword and then as sftp, that means you should use sftp.lisdir().

Hozayfa El Rifai
  • 1,432
  • 7
  • 17