0

Im trying to combine all .txt files in a directory and output them after the last one.

I have following Filesystem structure:

objects

   object1

       attribute1.txt

       attribute2.txt

       attribute3.txt

   object2

       attribute1.txt

       attribute2.txt

       attribute3.txt

and so on...

I've looked up this code

for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        # collect the information

I am looking for a for loop like

for subdir, dirs, files in os.walk(rootdir):  # <- what do I need to change here?
    for object in objects:  # <- how to implement this line correctly?
        for file in files:
            # collect the information
        print(information)

But I have to idea how to do that, since I am very new to python.

EDIT:

Python concatenate text files does not answer my question, since there is not an actual loop but only an Array with file names.

Huondui
  • 383
  • 1
  • 3
  • 12
  • Possible duplicate of [Python concatenate text files](https://stackoverflow.com/questions/13613336/python-concatenate-text-files) – wwii Aug 23 '19 at 13:33

2 Answers2

0

The first loop you show will already go through all the files. And the path to the files is stored in subdir

If you run:

for subdir, dirs, files in os.walk(rootdir):
    block=""
    for file in files:
        block+=subdir+"/"+file
    print block

You'll see that you get all your files. So now instead of the print statement put the command you want to read the files and store the content to a variable (you should read files with the path subdir+"/"+file).

Chelmy88
  • 1,106
  • 1
  • 6
  • 17
  • I know, Im using **os.path.join(subdir, file)** but I dont know to to print that **"block"** of information after I got all the information from the first object – Huondui Aug 23 '19 at 13:44
  • I don't get what you mean, sorry. What do you mean by "print that block of information". If you want to concatenate the files, once you are in the second loop, just read the content of the file and append it to a variable you created beforehand. – Chelmy88 Aug 23 '19 at 13:47
  • I have depicted my Filesystem in my question. I want to collect the information from **object1/attribute1.txt,attribute2.txt,attribute3.txt** and print it, after it is printed I want to continue with **object2/attribute1.txt, attribute2.txt, attribute3.txt** and print it aswell, same procedure for every object in that directory – Huondui Aug 23 '19 at 13:52
  • 1
    See the updated answer, the inner loop will loop over the files in one sub directory. If your `rootdir` is `object`, the outer loop will loop over `object1`, `object2`, etc. Is it what you need? Instead of `block+=subdir+"/"+file` you need to store in `block` the content of your files – Chelmy88 Aug 23 '19 at 13:58
0

You need to store the output of your for loop somewhere. This is an important thing to learn =)

my_files = [] #empty list

for file in directory:
    my_files.append(file) # each iteration adds to the list 

print(my_files)
Kermit
  • 4,922
  • 4
  • 42
  • 74
  • How do I **print(my_files)** after the first object and then after the second ... for each one of them – Huondui Aug 23 '19 at 13:45
  • Where would I need to put the print – Huondui Aug 23 '19 at 13:46
  • 1
    To be honest, this is a stretch. You are trying to force a piece of code you don't understand. You should really be storing the output in a nested Python `dictionary`. – Kermit Aug 23 '19 at 13:49
  • I've tried that in my previous question but it did not work https://stackoverflow.com/questions/57614547/how-to-create-a-dictionary-from-multiple-files-in-python-and-post-it-via-http?noredirect=1#comment101708137_57614547 I did not want to link it in first place because this question is not supposed to be spam for the other one – Huondui Aug 23 '19 at 13:54