Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8491205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:20:53+00:00 2026-06-10T22:20:53+00:00

I am using Python and trying to connect to SFTP and want to retrieve

  • 0

I am using Python and trying to connect to SFTP and want to retrieve an XML file from there and need to place it in my local system. Below is the code:

import paramiko

sftpURL   =  'sftp.somewebsite.com'
sftpUser  =  'user_name'
sftpPass  =  'password'

ssh = paramiko.SSHClient()
# automatically add keys without requiring human intervention
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )

ssh.connect(sftpURL, username=sftpUser, password=sftpPass)

ftp = ssh.open_sftp()
files = ftp.listdir()
print files

Here connection is success full. And now I want to see all the folders and all the files and need to enter in to required folder for retrieving the XML file from there.

Finally my intention is to view all the folders and files after connecting to SFTP server.

In the above code I had used ftp.listdir() through which I got output as some thing like below

['.bash_logout', '.bash_profile', '.bashrc', '.mozilla', 'testfile_248.xml']

I want to know whether these are the only files present?

And the command I used above is right to view the folders too?

What is the command to view all the folders and files?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:20:55+00:00Added an answer on June 10, 2026 at 10:20 pm

    One quick solution is to examine the output of lstat of each object in ftp.listdir().

    Here is how you can list all the directories.

    >>> for i in ftp.listdir():
    ...     lstatout=str(ftp.lstat(i)).split()[0]
    ...     if 'd' in lstatout: print i, 'is a directory'
    ... 
    

    Files are the opposite search:

    >>> for i in ftp.listdir():
    ...     lstatout=str(ftp.lstat(i)).split()[0]
    ...     if 'd' not in lstatout: print i, 'is a file'
    ... 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to download a file from a website using python and mechanize.
I am trying to plot two columns from a text file using python matplotlib
I'm trying to setup a local database on my laptop using python. I saw
I'm trying to connect to my phone from my Windows 7 PC using PySerial
I am trying to export a public key from openssl using python. I have
I'm trying to build a video thumbnailer using gst-python, it looks like this. from
I'm trying to use python to sftp a file, and the code works great
I'm trying to connect to a Sybase database in Python (using the python-sybase DBAPI
I am trying to get information from an SQL database using python I was
I'm trying to write simple graphic editor using PyGObject and python 3. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.