pysftp.connection.cd()
工作时遇到问题-不确定我做错了什么或问题是什么。我可以通过将完整的远程路径传递到相关方法中来实现我所需要的。但如果尝试更改远程Active Directory,我会出错。相关代码如下所示:
with ps.Connection('hostname or IP address', username='username', password='secret', cnopts=cnopts) as conn:
print("Connection successful....")
print(conn.listdir()) # this works
print(conn.listdir(remote_location)) # this works
with conn.cd(remote_location): # this throws an error
print(conn.listdir())
我得到的错误如下:
File "C:\Users\username\.conda\envs\data_wrangling\lib\site-packages\pysftp\__init__.py", line 508, in cd
self.cwd(remotepath)
File "C:\Users\username\.conda\envs\data_wrangling\lib\site-packages\pysftp\__init__.py", line 524, in chdir
self._sftp.chdir(remotepath)
File "C:\Users\username\.conda\envs\data_wrangling\lib\site-packages\paramiko\sftp_client.py", line 659, in chdir
if not stat.S_ISDIR(self.stat(path).st_mode):
OverflowError: mode out of range
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\username\my_code\python\mears_to_er_interface\sftp_chdir_test.py", line 41, in <module>
with conn.cd("remote_location"):
File "C:\Users\username\.conda\envs\data_wrangling\lib\contextlib.py", line 112, in __enter__
return next(self.gen)
File "C:\Users\username\.conda\envs\data_wrangling\lib\site-packages\pysftp\__init__.py", line 511, in cd
self.cwd(original_path)
File "C:\Users\username\.conda\envs\data_wrangling\lib\site-packages\pysftp\__init__.py", line 524, in chdir
self._sftp.chdir(remotepath)
File "C:\Users\username\.conda\envs\data_wrangling\lib\site-packages\paramiko\sftp_client.py", line 659, in chdir
if not stat.S_ISDIR(self.stat(path).st_mode):
OverflowError: mode out of range
conn.stat(remote_location).st_mode
的输出为83448
。
1条答案
按热度按时间snz8szmq1#
Paramiko在
SFTPClient.chdir
实现中使用Pythonstat
模块来检查路径是否是文件夹(而不是文件)。问题是Python
stat
模块只能使用16位权限,SFTP服务器返回的权限设置为17位,这会导致OverflowError
,对此您无能为力,请参阅Python的这篇文章:stat.S_ISXXX can raise OverflowError for remote file modes .只要避免使用
SFTPClient.chdir
,在Paramiko API中的任何地方都使用绝对路径,而不是依赖于使用chdir
设置的相对于工作目录的路径。Paramiko目前使用
stat
模块的唯一其他地方是SFTPAttributes.__str__
,否则你应该是安全的。我已经向帕拉米科项目报告了:https://github.com/paramiko/paramiko/issues/1802
如果您想分析自己代码中的权限位,只需屏蔽掉多余的位即可。
"OverflowError: mode out of range" in Python when working with file mode/attributes returned by Paramiko