【问题标题】:python check_output FileNotFoundError: [WinError 2] The system cannot find the file specifiedpython check_output FileNotFoundError: [WinError 2] 系统找不到指定的文件
【发布时间】:2020-01-02 01:45:12
【问题描述】:

我正在运行下一段代码来检查我的文件类型:

import subprocess as sub
output = sub.check_output(["file", "test.py"]).decode('ascii')   
#output=sub.check_output(["file","C:/Users/Roger.That/PycharmProjects/test/test.py"]).decode('ascii')

我不断收到以下错误:

Traceback (most recent call last):

  File "C:/Users/Roger That/PycharmProjects/test/test.py", line 2, in <module>
    output = sub.check_output(["file", "test.py"]).decode('ascii')
  File "C:\Users\Roger That\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 395, in check_output
    **kwargs).stdout
  File "C:\Users\Roger That\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 472, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Users\Roger That\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:\Users\Roger That\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

我的 test.py 的完整路径:"C:/Users/Roger.That/PycharmProjects/test/test.py"

gitbash 的输出:

$ file /c/Users/Roger\ That/PycharmProjects/test/test.py
/c/Users/Roger That/PycharmProjects/test/test.py: ASCII text, with CRLF line terminators

这是否与我在“Roger”和“That”之间有一个点有关?虽然当我使用相对路径(仅文件名)时它也不起作用

更新

我将用户的目录名称从 Roger that 更改为 Roger.that,但仍然没有帮助:

python /c/Users/Roger.That/PycharmProjects/test/test.py
same error..

也检查了:

import subprocess as sub
output = sub.check_output(["ls" "-l"])

遇到同样的错误

【问题讨论】:

  • 您似乎正在尝试运行一个名为“file”的可执行文件,但没有这样的文件。
  • @zvone 我已经安装了 gitbash,我可以使用 file 命令。我没有使用 shell=True 所以我想它应该可以工作..
  • "file" 可能在 gitbash 路径中,但不在 windows cmd 路径中。
  • 为什么不简单地使用python 运行脚本而不是依赖file?像这样:sub.check_output([sys.executable, "test.py"])
  • 我的目标是分析file命令的输出

标签: python subprocess


【解决方案1】:

我从头开始创建了虚拟环境,它解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    相关资源
    最近更新 更多