即使在subprocess.Popen中设置了close_fds=True,Python也会打开一个文件两次
我在subprocess.Popen中使用close_fds=True,但它仍然会打开文件两次。我搜索了很多,但我找不到解决这个问题的方法 我想打开一个文件并截图即使在subprocess.Popen中设置了close_fds=True,Python也会打开一个文件两次,python,python-2.7,subprocess,popen,Python,Python 2.7,Subprocess,Popen,我在subprocess.Popen中使用close_fds=True,但它仍然会打开文件两次。我搜索了很多,但我找不到解决这个问题的方法 我想打开一个文件并截图 import os import pyscreenshot as ImageGrab import subprocess as sp import time def imagegrabber(x=0): if(x==1 and __name__ == '__main__'):
import os
import pyscreenshot as ImageGrab
import subprocess as sp
import time
def imagegrabber(x=0):
if(x==1 and __name__ == '__main__'):
# part of the screen
im=ImageGrab.grab(bbox=(50,70,500,500))
im.show()
# to file
ImageGrab.grab_to_file('im.png')
return;
def Dashboard(x=0):
if(x==1):
mcr = "D:/MCR/test.pbix"
path = r'C:\Program Files\Microsoft Power BI Desktop\bin\PBIDesktop.exe'
sp.Popen([path,mcr],close_fds=True)
time.sleep(10)
imagegrabber(1)
return;
Dashboard(1)
我只需要在Dashboard函数中添加if(name='main'),而不是在imagegrabber函数中。休息一切正常。你能解释一下你看到什么让你说它正在打开文件两次吗?电源BI正在打开,几秒钟后出现一个弹出框,上面写着“D:/MCR/test.pbix无法打开,因为它已经打开了”。愚蠢的问题,但是,当您试图运行此程序时,是否在编辑器或其他地方打开了该文件?否。该文件仅由python脚本打开,之后我将收到此消息。我试着在重启系统后运行代码。问题解决了。我必须只在仪表板中添加if(name=='main')条件