【问题标题】:Python subprocess File not found [duplicate]找不到Python子进程文件[重复]
【发布时间】:2013-10-28 05:18:41
【问题描述】:

我正在尝试使用带有子进程的文件来查找文件类型

    cwdir = os.getcwd()
    Fileinput=cwdir+"/"+'testfile.zip'
    print "Current Directory %s"% cwdir
    Fileformat=subprocess.Popen('file' + Fileinput)

我得到 OSError: [Errno 2] No such file or directory。我验证并且该文件确实存在于路径中。 感谢您对此的任何帮助。

【问题讨论】:

    标签: python subprocess


    【解决方案1】:

    'file'fileinput 之间添加空格

    Fileformat = subprocess.Popen('file ' + Fileinput)
    #                                  ^
    

    否则,file/current/path/testfile.zip 将被视为可执行路径,而不是 file

    或使用以下形式:

    Fileformat = subprocess.Popen(['file', Fileinput])
    

    如果你想获得命令的输出,你必须将stdout=subprocess.PIPE 传递给Popen 并使用Fileformat.stdout.read() 读取。

    subprocess.check_output怎么样?

    >>> subprocess.check_output(['file', '/etc/passwd'])
    '/etc/passwd: ASCII text\n'
    

    【讨论】:

    • 即使在为“文件”添加空间后,我仍然会收到相同的错误。但是我将其更改为 check_output 并且它可以工作。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 2013-06-25
    • 2022-11-23
    相关资源
    最近更新 更多