2016-11-17 48 views
0

我米使用的是Mac,並增加了對桌面RSA密鑰錯誤的RSA密鑰的paramiko LIB

我使用的路徑是

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

錯誤:

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' 

回答

2

你不能直接在文件的路徑中使用~。這是一個shell功能,並通過shell進行了擴展。

使用os.path.expanduser(path)可以在使用文件路徑之前擴展文件路徑中的~

1

您不能在Python路徑中使用~。嘗試硬編碼您的主目錄,它會起作用。

,如果你想使用波浪號(~

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

'expanduser()'可以直接使用,如果路徑與'〜'開始可以使用expanduser()。沒有必要將路徑拆分成組件並在以後重新組裝。 –