I am new to Python and I am using it to do some data analysis.
My problem is the following: I have a directory with many subdirectories, each one of which contains a large number of data files.
I already wrote a Python script which, when executed in one of those subdirectories, performs the data analysis and writes it on a output file. The script includes some shell commands that I call using
To change your working directory in Python you need:
You can then recursively run your script.
import os directory_to_check = "your_dir" # Which directory do you want to start with? def my_function(directory): print("Listing: " + directory) print("\t-" + "\n\t-".join(os.listdir("."))) # List current working directory # Get all the subdirectories of directory_to_check recursively and store them in a list: directories = [os.path.abspath(x) for x in os.walk(directory_to_check)] directories.remove(os.path.abspath(directory_to_check)) # If you don't want your main directory included for i in directories: os.chdir(i) # Change working Directory my_function(i) # Run your function
I don't know how your script works because your question is quite general, so therefore I can only give a general answer....
But I think what you need is:
os.walk alone won't work
I hope this helps! Good luck!