简体   繁体   English

在MAC上用Python调用Popen-错误找不到文件

[英]Calling Popen in Python on a MAC - Error can not find file

When I try to shell out of my Python 3.51 program to run the Popen command I get the following errors. 当我尝试退出Python 3.51程序以运行Popen命令时,出现以下错误。 Yet when I copy the exact string I'm passing to Popen to the Terminal command line it works fine and opens the file in Adobe Reader which is my default app for the .pdf files. 但是,当我将传递给Popen的确切字符串复制到Terminal命令行时,它可以正常工作,并在Adobe Reader中打开该文件,这是我的.pdf文件的默认应用程序。

Here is the Code: 这是代码:

  finalCall = r'open /Users/gbarnabic/Documents/1111/combined.pdf' print(finalCall) pid_id = subprocess.Popen(finalCall).pid 

Here is the error: 这是错误:

open /Users/gbarnabic/Documents/1111/combined.pdf Exception in Tkinter callback Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/ init .py", line 1549, in call return self.func(*args) File "pdfcomb2.py", line 212, in change_dir self.openPDF(outFileName, pageNum) File "pdfcomb2.py", line 426, in openPDF subprocess.run(finalCall) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 696, in run with Popen(*popenargs, **kwargs) as process: File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 950, in init restore_signals, start_new_session) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 1544, in _execute_child raise child_exception_type(errno_num, err_msg) FileNotFoundError: [Errno 2] No such file or directory: 'open /Users/gb/Documents/1111/combined.pdf' Georges-MBP:filepicktest gb$ open /Users/gb/Documents/1 打开/Users/gbarnabic/Documents/1111/combined.pdf Tkinter回调Traceback中的异常(最近一次调用最近):文件“ /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/ init 。 py”,在调用返回1549行,返回self.func(* args)文件,“ pdfcomb2.py”,在212行,在change_dir self.openPDF(outFileName,pageNum)文件,“ pdfcomb2.py”,在426行,在openPDF子进程中。运行(finalCall)文件“ /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py”,行696,以Popen(* popenargs,** kwargs)作为进程运行:“文件” /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py“,第950行, init restore_signals,start_new_session)文件“ /Library/Frameworks/Python.framework/Versions/3.5/lib/ python3.5 / subprocess.py“,行1544,在_execute_child中引发child_exception_type(errno_num,err_msg)FileNotFoundError:[Errno 2]没有这样的文件或目录:'open /Users/gb/Documents/1111/combined.pdf'Georges- MBP:filepicktest gb $ open / Users / gb / Documents / 1 111/combined.pdf Georges-MBP:filepicktest gb$ 111 / combined.pdf Georges-MB​​P:filepicktest gb $

With Popen you need to set shell=True to pass command as a string or split command in a list of arguments. 使用Popen时,您需要将shell=True设置为在字符串列表中将命令作为字符串或split命令传递。 Could be done with shlex 可以用shlex完成

import shlex
import subprocess
subprocess.Popen(shlex.split('open ....'))

You could check example in documentation: https://docs.python.org/2/library/subprocess.html#subprocess.Popen 您可以在文档中查看示例: https : //docs.python.org/2/library/subprocess.html#subprocess.Popen

So the error here means that Python try to run file with name open /Users/gb/Documents/1111/combined.pdf . 因此这里的错误意味着Python尝试运行名称为open /Users/gb/Documents/1111/combined.pdf文件。 Obviously it doesn't exist 显然不存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM