Skip to content

Instantly share code, notes, and snippets.

@Makistos
Created February 1, 2017 14:34
Show Gist options
  • Save Makistos/9b3279317223917dfd3d971fbbef8736 to your computer and use it in GitHub Desktop.
Save Makistos/9b3279317223917dfd3d971fbbef8736 to your computer and use it in GitHub Desktop.
Read files recursively. Useful for e.g. command files. #python #file-handling
def parse_test_file(filename):
retval = []
with open(filename) as f:
lines = f.read().splitlines()
path, script = os.path.split(os.path.abspath(filename))
cwd = os.getcwd()
if cwd != path:
os.chdir(path)
for line in lines:
if line.startswith('/include'):
retval = retval + parse_test_file(line[9:])
else:
retval.append(line)
if cwd != path:
os.chdir(cwd)
return retval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment