I have the following command I'm running on my remote server
python -c 'import os, json; print json.dumps(os.listdir("."))'
This works fine for listing files/directories in the current directory however how would I change this to follow directories and list the containing files?
Python, ever eager to please, provides a standard library function for that. os.walk wraps up the pattern of recursively listing files in subdirectories.
Here's how you could json-ify a list of all the files in this directory or any subdirectories. I'm using a two-level list comprehension to concatenate the lists of files:
import json
import os
print(json.dumps([file for root, dirs, files in os.walk('.') for file in files]))
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