2016-11-17 49 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()。没有必要将路径拆分成组件并在以后重新组装。 –