[英]Calling Popen in Python on a MAC - Error can not find file
當我嘗試退出Python 3.51程序以運行Popen命令時,出現以下錯誤。 但是,當我將傳遞給Popen的確切字符串復制到Terminal命令行時,它可以正常工作,並在Adobe Reader中打開該文件,這是我的.pdf文件的默認應用程序。
這是代碼:
finalCall = r'open /Users/gbarnabic/Documents/1111/combined.pdf' print(finalCall) pid_id = subprocess.Popen(finalCall).pid
這是錯誤:
打開/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 $
使用Popen時,您需要將shell=True
設置為在字符串列表中將命令作為字符串或split命令傳遞。 可以用shlex
完成
import shlex
import subprocess
subprocess.Popen(shlex.split('open ....'))
您可以在文檔中查看示例: https : //docs.python.org/2/library/subprocess.html#subprocess.Popen
因此這里的錯誤意味着Python嘗試運行名稱為open /Users/gb/Documents/1111/combined.pdf
文件。 顯然不存在
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.