1

I want to make an abstract script, that will activate itself every hour and run all the scripts from the specific directory. It doesn't matter if there's two of them or a dozen.

When I run

import os

for item in os.listdir(r'C:/directory/Desktop'):
     execfile(item)

It says that there's no such file, even though if I list them (with print instead of execfile) I see all of them. I decided to get the exact directory of each of them.

import os

for item in os.listdir(r'C:/directory/Desktop'):
     execfile(r'C:/directory/Desktop/%s'%item)

It stops after running the first script that was found. Let's make an unstoppable while loop then.

import os

script_list = []

for item in os.listdir(r'C:/directory/Desktop'):
    script_list.append(item)

while len(script_list) > 0:
    execfile(r'C:/directory/Desktop/%s'%(script_list.pop()))

How surprised I was when it didn't work either. Once again, only the first script that was found has been executed.

So, the question is, do you guys know how to run all scripts in loop in a specific dir without knowing their names?

In each of these scripts I use

return sys.exit(function)

May this cause this issue?

I tried with subprocess.call(item) and run(item) with no luck.

1
  • You could just call python /dir/*.py via subprocess or similar; or import all the modules and call them Commented Jan 9, 2017 at 15:28

2 Answers 2

1

subprocess.call is the correct way to go here, from my experience. If all you are running is .py files, i think the problem has to do with not having python in your call array. e.g.,

subprocess.call(['c:/path/to/python', script_list.pop()])

And let me second @Chris_Rands here, I strongly prefer this being done as a python module with a marker class. i.e.,

directory
-- __init__.py
-- script1.py
-- script2.py
-- script3.py

with script1.py, script2.py, etc., defining a a run method on a class that subclasses a marker class like Runnable. Then you can use the following code to run all Runnables in a given directory:

module = import_module('directory')
for name, klass in inspect.getmembers(module):
    if name.startswith('__') or not inspect.isclass(klass): continue
    instance = klass()
    run_fn = getattr(instance, 'run', None)
    if run_fn and callable(run_fn):
        run_fn()
Sign up to request clarification or add additional context in comments.

1 Comment

It looks nice but I don't have enough skill to create my own abstract module and there are a few things that I don't get in your code. But I promise that I'll get into it in half of a year! Anyway, I'm trying to execute this with subprocess. All of them are .py scripts. I forgot to write it, whenever I use subprocess.call I get WindowsError: [Error 193] %1 is not a valid Win32 application. But I think that I'll just stop here, I want to run it on Rpi and currently I'm on Windows, so maybe it's smarter to test the code on the Pi itself. Thank you for your answer.
1

I do not have the same issue on my linux FS. Thus I can only try to give you some code to work with:

What about this:

import os
from threading import Thread

path = "folder/path"
for item in os.listdir(path):

    def execFile():
        execfile("{:s}/{:s}".format(path,item))

    thread = Thread(target = execFile)
    thread.start()
    thread.join()

1 Comment

I'm using api to get data and for some reason it raises exceptions in thread Thread-1 and Thread-2. It says that the object that gets the API is not defined. Anyway it might be caused by Windows and I want to run it on rpi in a week or so. There might be a lot of more bugs caused by this cross-os. Thanks anyway, I'll look more into this option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.