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 6376053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:41:43+00:00 2026-05-25T01:41:43+00:00

I have some Python code that uses Paramiko to grab build files from a

  • 0

I have some Python code that uses Paramiko to grab build files from a remote server:

def setup_sftp_session(self, host='server.com', port=22, username='puppy'):
    self.transport = paramiko.Transport((host, port))
    privatekeyfile = os.path.expanduser('~/.ssh/id_dsa')
    try:
        ssh_key = paramiko.DSSKey.from_private_key_file(privatekeyfile)
    except IOError, e:
        self.logger.error('Unable to find SSH keyfile: %s' % str(e))
        sys.exit(1)
    try:
        self.transport.connect(username = username, pkey = ssh_key)
    except paramiko.AuthenticationException, e:
        self.logger.error("Unable to logon - are you sure you've added the pubkey to the server?: %s" % str(e))
        sys.exit(1)
    self.sftp = paramiko.SFTPClient.from_transport(self.transport)
    self.sftp.chdir('/some/location/buildfiles')

def get_file(self, remote_filename):
    try:
        self.sftp.get(remote_filename, 'I just want to save it in the local cwd')
    except IOError, e:
        self.logger.error('Unable to find copy remote file %s' % str(e))

def close_sftp_session(self):
    self.sftp.close()
    self.transport.close()

I’d like to retrieve each file, and deposit it in the current local working directory.

However, Paramiko doesn’t seem to have an option for this – you need to specify the full local destination. You can’t even specify a directory (e.g. “./”, or even “/home/victorhooi/files”) – you need the full path including filename.

Is there any way around this? It’ll be annoying if we have to specify the local filename as well, instead of just copying the remote one.

Also – the way I’m handling exceptions in setup_sftp_session, with exit(1) – is that a good practice, or is there a better way?

Cheers,
Victor

  • 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-05-25T01:41:43+00:00Added an answer on May 25, 2026 at 1:41 am

    you have to insert

    os.path.join(os.getcwd(), remote_filename)
    

    Calling exit() in a function is not a good idea. Maybe you want to reuse the code and take some action in case of an exception. If you keep the exit() call you are lost.
    I suggest to modify this function such that a True is returned in case of success, and False else. Then the caller can decide what to do.

    Another approach would be not to catch the exceptions. So the caller has to handle them and the caller gets the full information (including the stacktrace) about the circumstances of the failure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some old python code that uses the pywin32 extensions. Starting out with
I have some python code that: Takes a BLOB from a database which is
I have some Python code that works correctly when I use python.exe to run
I'm looking at existing python code that heavily uses Paramiko to do SSH and
I'm trying to test some python code that uses urllib2 and lxml. I've seen
I'm writing some python code to interact with a C DLL that uses structures
I am hoping to port some Python code that uses RPY2 to run R
I have some python code that does a certain task. I need to call
I have some Python code that's dependent upon passing around some lambdas, and they
I have some python code that binds to a socket. I catch any exception

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.