3

I have made a search of python scripts in some subfolders using os.walk() and endswith(file.py), and put them in a list. Now I'm trying to execute them. So I have a list of the type:

pylist = ['./1/file1.py', './2/file2.py', ...]

and I have tried to execute them using a for loop:

for i in range(len(pylist)):
    %run pylist[i]

and also (I'm using a jupyter notebook)

for i in range(len(pylist)):
    !python pylist[i]

but in both cases it doesn't find the file pylist[i].py, so it's not taking the content of the list. I also tried

for i in range(len(pylist)):
    execfile(pylist[i])

but this gives me an

indexError: list index out of range

with one of the python files, which I don't get if I go directly to the folder and execute the file. One more test was using subprocess, for which I got a permission error:

OSError: [Errno 13] Permission denied

that I suspect it might be because the .py files doesn't have executable permissions. But then, make each file a executable is another problem.

What is the correct way of execute python files in a list?

Edit: More specific

When going to a specific directory on my jupyter notebook and executing the file as

!python file1.py

it works fine, but when using a list I get

for fname in pylist:    
    !python fname

python: can't open file 'fname': [Errno 2] No such file or directory

so it seems like python is not reading the elements on the list but trying to execute fname. I checked with os.path.abspath(fname) that the paths are correct.

EternalGenin
  • 495
  • 2
  • 6
  • 14

1 Answers1

1

You have a few issues going on with your loop. There is no need to get the len, then look up the index. A for...in... loop will get the actual file string. You can use the subprocess module to make a call.

import subprocess

files = ['./py1.py', './py2.py']

for fname in files:

    subprocess.call([fname])


print("done")

the contents of py1 and py2 that I had a hashbang and a print:

#!/usr/bin/env python

# py1.py
print("test 1")

Finally, you can always import the files, but doing this with a list may be challenging.

>>>import py1
test 1
ryati
  • 360
  • 3
  • 13
  • Did you give permissions to your py1 and py2? e.g. `chmod +x py1.py`? Because this is not working for me, I'm getting a "permission denied" error. – EternalGenin Feb 20 '18 at 15:48
  • I did not explicitly give the files permissions, however on inspection, it looks like they all have exe permission. It must have inherited it. – ryati Feb 20 '18 at 17:08
  • perhaps a noob question, but when I run `subprocess.call("test.py")` from a script in the same directory as test.py, it gives an error about not finding the file. To be extra explicit, I have caller.py and test.py in same directory, caller is running the above code. Any idea what I'm doing wrong? – Adam B Feb 22 '20 at 05:19
  • OMG, I am a noob indeed. For future readers: `subprocess.call(['python', 'test.py'])`. Need to invoke python to run it since it's not in the path. – Adam B Feb 22 '20 at 05:23