找不到Python子进程文件

2024-10-02 14:20:08 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我得到OSError:[Errno 2]没有这样的文件或目录。我验证了,文件确实存在于路径中。 谢谢你的帮助。在


Tags: 文件类型进程oscurrentzipdirectorysubprocess
1条回答
网友
1楼 · 发布于 2024-10-02 14:20:08

'file'和{}之间添加空格

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

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

或使用以下表格:

^{pr2}$

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

使用subprocess.check_output怎么样?在

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

相关问题 更多 >