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.