gpt4 book ai didi

Python子进程文件未找到

转载 作者:行者123 更新时间:2023-12-01 05:33:33 25 4
gpt4 key购买 nike

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

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

我收到 OSError: [Errno 2] 没有这样的文件或目录。我验证了该文件确实存在于路径中。感谢您对此提供的任何帮助。

最佳答案

'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'

关于Python子进程文件未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19627627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com