Reputation: 65
so I'm a rookie at programming and I'm trying to make a program in python that basically opens a text file with a bunch of columns and writes the data to 3 different text files based on a string in the row. As my program stands right now, I have it change the directory to a specific output folder using os.chdir
so it can open my text file but what I want is it to do something like this:
Imagine a folder set up like this :
Source
Folder contains N
number of folders. Each of those folders contains N
number of output folders. Each output folder contains 1 Results.txt
.
The idea is to have the program start at the source folder, look into Folder 1
, look for output 1
, open the .txt
file then do it's thing. Once it's done, it should go back to folder 1
and open output 2
and do it's thing again. Then it should go back to Folder 1
and if it can't find any more output folders, it needs to go to Folder A
and then enter Folder 2
and repeat the process until there are no more folders. Honestly not sure where to really start with this, the best I could do is make a small program that prints all my .txt files but I'm not sure how to open them at all. Hope my question makes sense and thanks for the help.
Upvotes: 0
Views: 934
Reputation: 5251
If all you need is to process each file in a directory recursively:
import os
def process_dir(dir):
for subdir, dirs, files in os.walk(dir):
for file in files:
file_path = os.path.join(subdir, file)
print file_path
# process file here
This will process each file in the root dir recursively. If you're looking for conditional iteration you might need to make the loop a little smarter.
Upvotes: 2
Reputation: 17256
I would do this without chdir
. The most straight forward solution to me is to use os.listdir
and filter the results. Then os.path.join
to construct complete relative paths instead of chdir
. I suspect this would be less prone to bugs such as winding up in an unexpected current working directory where all your relative paths are then wrong.
nfolders = [d for d in os.listdir(".") if re.match("^Folder [0-9]+$", d)]
for f1 in nfolders:
noutputs = [d for d in os.listdir(f1) if re.match("^Output [0-9]+$", d)]
for f2 in noutputs:
resultsFilename = os.path.join(f1, f2, "results.txt")
#do whatever with resultsFilename
Upvotes: 0
Reputation: 582
first, i think you could format your question for better reading.
Concerning your question, here's a naïve implementation example :
import os
where = "J:/tmp/"
what = "Results.txt"
def processpath(where, name):
for elem in os.listdir(where):
elempath = os.path.join(where,elem)
if (elem == name):
# Do something with your file
f = open(elempath, "w") # example
f.write("modified2") # example
elif(os.path.isdir(elempath)):
processpath(elempath, name)
processpath(where, what)
Upvotes: 0
Reputation: 838
dirlist = os.listdir(os.getcwd())
dirlist = filter(lambda x: os.path.isdir(x), filelist)
for dirname in dirlist:
print os.path.join(os.getcwd(),dirname,'Results.txt')
Upvotes: 0
Reputation: 924
Read the base folder path and stored into variable and move to sub folder and process the text file using chdir and base path change the directory and read the sub folder once again.
Upvotes: 0