I had connected to sftp with some details to fetch some files with paramiko from python and code is below
import paramiko
import os
import ftplib
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
ssh.connect('sftp.example.com', username='user', password='pass')
ftp = ssh.open_sftp()
files = ftp.listdir()
print files,"----< Total files"
print ftp.cwd(),"---< Current working diectory"
for feedFile in files:
if feedFile == 'LifeBridge':
ftp.chdir(feedFile)
Result
['Emerson', 'Lehighvalley', 'LifeBridge', 'stmaryct'] ----< Total files
File "test_sftp.py", line 11, in <module>
print ftp.cwd() ---< Current working diectory
AttributeError: 'SFTPClient' object has no attribute 'cwd'
here what i am doing is
Trying to find the current working directory of sftp
There is a list of files above which i got as a result for printing files, i am trying to check whether they are folders or files,
If they are folders i want to enter in to LifeBridge
folder
Finally can anyone let me know the following
You are looking for the .getcwd()
method instead:
Return the "current working directory" for this SFTP session, as emulated by paramiko. If no directory has been set with
chdir
, this method will returnNone
.
So at the start of your session, if you need the current working dir, try a .chdir('.')
(change to current directory) first.
The .listdir()
method only returns names of items in the directory. If you need more information about those files, you'd need to use the .listdir_attr()
method instead, it returns a list of SFTPAttributes
instances.
An SFTPAttributes
instance tells you what mode the file has, through the .st_mode
attribute. Use the stat.S_ISDIR(mode)
function to test if the file is a directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With