Search code examples
pythonpython-3.xfor-loopin-place

Modifying a file in-place inside nested for loops


I am iterating directories and files inside of them while I modify in place each file. I am looking to have the new modified file being read right after. Here is my code with descriptive comments:

# go through each directory based on their ids
for id in id_list:
    id_dir = os.path.join(ouput_dir, id)
    os.chdir(id_dir)

    # go through all files (with a specific extension)
    for filename in glob('*' + ext):

        # modify the file by replacing all new-line characters with an empty space
        with fileinput.FileInput(filename, inplace=True) as f:
            for line in f:
                print(line.replace('\n', ' '), end='')

        # here I would like to read the NEW modified file
        with open(filename) as newf:
            content = newf.read()

As it stands, the newf is not the new modified one, but instead the original f. I think I understand why that is, however I found it difficult to overcome that issue.

I can always do 2 separate iterations (go through each directory based on their ids, go through all files (with a specific extension) and modify the file, and then repeat iteration to read each one of them) but I was hoping if there was a more efficient way around it. Perhaps if it would be possible to restart the second for loop after the modification has taken place and then have the read take place (so to avoid at least repeating the outer for loop).

Any ideas/designs of to achieve the above with a clean and efficient way?


Solution

  • For me it works with this code:

    #!/usr/bin/env python3
    import os
    from glob import glob
    import fileinput
    
    id_list=['1']
    ouput_dir='.'
    ext = '.txt'
    # go through each directory based on their ids
    for id in id_list:
        id_dir = os.path.join(ouput_dir, id)
        os.chdir(id_dir)
    
        # go through all files (with a specific extension)
        for filename in glob('*' + ext):
    
            # modify the file by replacing all new-line characters with an empty space
            for line in  fileinput.FileInput(filename, inplace=True):
                print(line.replace('\n', ' ') , end="")
    
            # here I would like to read the NEW modified file
            with open(filename) as newf:
                content = newf.read()
            print(content)
    

    notice how I iterate over the lines!