【问题标题】:Python opens a file twice even after seting up close_fds=True in subprocess.Popen即使在 subprocess.Popen 中设置 close_fds=True 后,Python 也会打开文件两次
【发布时间】:2017-06-04 06:18:45
【问题描述】:

我在 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__'):
            # 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)

【问题讨论】:

  • 你能解释一下你所看到的让你说它打开文件两次的原因吗?
  • Power BI 正在打开,几秒钟后会出现一个弹出框,提示“D:/MCR/test.pbix 已打开,无法打开”。
  • 愚蠢的问题,但是当您尝试运行此文件时,您是否在编辑器中打开了该文件?
  • 没有。该文件仅由 python 脚本打开,之后我收到此消息。我也尝试在重新启动系统后运行代码。
  • 解决了。我必须只在仪表板中添加 if( name == 'main') 条件

标签: python python-2.7 subprocess popen


【解决方案1】:

我只需要在 Dashboard 函数中添加 if(name == 'ma​​in') 而不是在 imagegrabber 函数中。休息就好了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2017-07-01
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多