【问题标题】:subprocess.Popen() read a filesubprocess.Popen() 读取文件
【发布时间】:2015-01-26 02:31:52
【问题描述】:

我正在尝试使用子进程来读取存储在远程服务器中的文件。

import subprocess
import sys

ssh = subprocess.Popen(['ssh', 'hjh:passwd@myserver', 'cat', 'data/test.txt'],
                   shell=False,
                   stdout=subprocess.PIPE,
                   stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
if result == []:
    error = ssh.stderr.readlines()
    print >>sys.stderr, "ERROR: %s" % error
else:
    print result

现在运行这会导致错误

Traceback (most recent call last):
  File "C:/Users/hjh/Desktop/try.py", line 15, in <module>
stderr=subprocess.PIPE)
  File "C:\Python27\lib\subprocess.py", line 710, in __init__
errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

这个错误是否意味着找不到服务器上的文件?还是子流程有错误? Google 在这个错误上对我帮助不大。

我还使用此处未指定的代理,这可能是个问题吗?

干杯,

【问题讨论】:

    标签: python ssh proxy subprocess


    【解决方案1】:

    我认为 Windows 无法找到 SSH 可执行文件。检查在cmd.exe 中键入ssh 时会发生什么。尝试在 Popen 调用中添加 SSH 客户端可执行文件的完整路径,或将 SSH 的目录添加到 PATH 环境变量。

    【讨论】:

    • 是的,当我更改 shell=True 时,它显示了 'ssh' is not recognized as an internal or external command 错误。所以我会解决这个问题。谢谢
    【解决方案2】:

    似乎“ssh”无法解析。我会在 Popen 构造函数调用中完全限定 ssh 可执行文件的位置和/或它在可搜索的 PATH 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      • 2011-08-10
      • 1970-01-01
      相关资源
      最近更新 更多