Python : get all exe files in current directory and run them?
Asked Answered
F

2

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

Frequency answered 10/6, 2010 at 12:2 Comment(5)
Why do you need to involve Python, can't you simply use the batch file to start those programs?Carolacarolan
We're adding tests daily, we don't want to edit the batch file every time we add something new, we need a flexible solutionFrequency
What error or problem were you getting with the code I posted?Roughhouse
@ Noctis I'll let you know tomorrow once I return to the officeFrequency
Have you found out what the problem was with my code?Roughhouse
R
9
import glob, os
def solution():
    for fn in glob.glob("*_text.exe"):
        os.startfile(fn)
Raja answered 10/6, 2010 at 12:13 Comment(4)
this solution worked. Another question that I have is : can I make the script wait till the current process finishes execution ?Frequency
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'Davenport
@gnibbler : what if i wanted to do this on the whole hard disk, rather than just the current directory.Mercurialism
@thecreator232, you can use os.walk to loop over all the directories on your hard disk.Raja
R
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.
Roughhouse answered 10/6, 2010 at 12:8 Comment(1)
there's a problem with this solution :)Frequency

© 2022 - 2024 — McMap. All rights reserved.