Vidit Shah
Vidit Shah

Reputation: 21

error in RSA key paramiko lib

I m using mac and have added rsa key on desktop

The path I 'm using is

host_key = paramiko.RSAKey(filename='~/Desktop/test_rsa.key')

Error:

Traceback (most recent call last):
  File "/Users/vidit/PycharmProjects/untitled6/server.py", line 7, in <module>
    host_key = paramiko.RSAKey(filename='~/Desktop/test_rsa.key')
  File "/Library/Python/2.7/site-packages/paramiko/rsakey.py", line 45, in __init__
    self._from_private_key_file(filename, password)
  File "/Library/Python/2.7/site-packages/paramiko/rsakey.py", line 163, in _from_private_key_file
    data = self._read_private_key_file('RSA', filename, password)
  File "/Library/Python/2.7/site-packages/paramiko/pkey.py", line 267, in _read_private_key_file
    with open(filename, 'r') as f:
IOError: [Errno 2] No such file or directory: '~/Desktop/test_rsa.key'

Upvotes: 0

Views: 663

Answers (2)

Hannu
Hannu

Reputation: 12205

You can't use ~ in Python paths. Try hardcoding your home directory and it will work.

You can use expanduser() if you want to use tilde (~)

from os.path import expanduser    
keypath = expanduser("~/Desktop/test_rsa.key")

Upvotes: 1

Klaus D.
Klaus D.

Reputation: 14369

You can not use ~ in the path of a file directly. It is a shell feature and expanded by the shell.

Use os.path.expanduser(path) to expand the ~ in the file path before using it.

Upvotes: 2

Related Questions