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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With