如何从同一个python脚本运行.py和.exe文件

时间:2014-01-23 14:18:58

标签: python numpy exe

我想知道如何从同一个python脚本运行.py和.exe文件。

我有一个用于运行.py的脚本和一个用于运行.exe的脚本。

我的问题是我不知道如何从同一个脚本运行它们!我想在两个.py文件之间发生exe文件。

.py脚本

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import division  # so that 1/3=0.333 instead of 1/3=0
from psychopy import visual, core, data, event, logging, sound, gui
from psychopy.constants import *  # things like STARTED, FINISHED
import numpy as np  # whole numpy lib is available, prepend 'np.'
from numpy import sin, cos, tan, log, log10, pi, average, sqrt, std, deg2rad, rad2deg, linspace, asarray
from numpy.random import random, randint, normal, shuffle
import os  # handy system and path functions

"""find and execute all .py files in the current directory"""

import glob, sys
import subprocess
import random


def launch(exp):
    """launch a compiled PsychoPy experiment (.py) via subprocess"""
    command = [sys.executable, '-u', exp] # spaces in file names are ok
    proc = subprocess.Popen(command)
    proc.communicate()
    del proc


expts = ['myPythonFile1.py', 'myPythonFile2.py']
for exp in expts:
    launch(exp)

.exe脚本

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import division  # so that 1/3=0.333 instead of 1/3=0
from psychopy import visual, core, data, event, logging, sound, gui
from psychopy.constants import *  # things like STARTED, FINISHED
import numpy as np  # whole numpy lib is available, prepend 'np.'
from numpy import sin, cos, tan, log, log10, pi, average, sqrt, std, deg2rad, rad2deg, linspace, asarray
from numpy.random import random, randint, normal, shuffle
import os  # handy system and path functions

import subprocess

a=subprocess.Popen(r"myEXEfile.exe")
a1=os.system(r"myEXEfile.exe")

1 个答案:

答案 0 :(得分:1)

只需检查每种类型。

def launch(exp):
    # check if exe or py
    if (exp.endswith(".exe")):                  # So it's an exe.
        a=subprocess.Popen(exp)
        a1=os.system(exp)
    else:                                       # Otherwise is a .py        
       # Launch a compiled PsychoPy experiment (.py) via subprocess.
        command = [sys.executable, '-u', exp] # spaces in file names are ok
        proc = subprocess.Popen(command)
        proc.communicate()
        del proc 

# And just put your exe filename between the two py files. 
expts = ['myPythonFile1.py', r'myEXEfile.exe', 'myPythonFile2.py']
for exp in expts:
    launch(exp)

此外,如果您不使用流程输出,我建议您使用proc.wait()代替proc.communicate