6

First of all this is not homework, I'm in a desperate need for a script that will do the following, my problem is, I've never had to deal with python before so I barely know how to use it - and I need it to launch unit tests in TeamCity via a commandline build runner

What I need exactly is :

a *.bat file that will run the script

a python script that will :

  • get all *_test.exe files in the current working directory
  • run all the files which were the result of the search

Best regards

Maciek
  • 19,435
  • 18
  • 63
  • 87

2 Answers2

9
import glob, os
def solution():
    for fn in glob.glob("*_text.exe"):
        os.startfile(fn)
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • this solution worked. Another question that I have is : can I make the script wait till the current process finishes execution ? – Maciek Jun 10 '10 at 12:18
  • 2
    add `import subprocess` and change `os.startfile(fn)` to `p = subprocess.Popen([fn]); p.wait()`. p.wait() will also give you the return code so you could do something like `if p.wait() == 0: print 'Success'; else: print 'Fail'` – Wayne Werner Jun 10 '10 at 12:53
  • @gnibbler : what if i wanted to do this on the whole hard disk, rather than just the current directory. – thecreator232 Oct 01 '13 at 02:17
  • 1
    @thecreator232, you can use `os.walk` to loop over all the directories on your hard disk. – John La Rooy Oct 01 '13 at 03:17
3

If you copy this into a file, the script should do as you asked.

import os       # Access the operating system.

def solution(): # Create a function for later.
    for name in os.listdir(os.getcwd()):
        if name.lower().endswith('_test.exe'):
            os.startfile(name)

solution()      # Execute this inside the CWD.
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117