在嵌套for循环内就地修改文件

时间:2016-06-15 14:37:07

标签: python python-3.x for-loop in-place

我正在迭代其中的目录和文件,同时我修改了每个文件。我希望能够立即读取新修改的文件。 这是我的代码,带有描述性注释:

# 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()

按照目前的情况,newf 新修改后的版本,而是原始f。我想我明白为什么会这样,但是我发现很难克服这个问题。

我总是可以进行2次单独的迭代(根据他们的ID查看每个目录,浏览所有文件(具有特定扩展名)并修改文件,然后重复迭代以阅读其中的每一个)但我希望如果有更有效的方法来解决它。也许如果在修改发生之后重新启动第二个for循环,然后发生read,那么可能是为了避免至少重复外部{{ {1}}循环)。

以干净有效的方式实现上述目标的任何想法/设计?

2 个答案:

答案 0 :(得分:1)

我并不是说你这样做的方式是不正确的,但我觉得你过于复杂了。这是我的超级简单解决方案。

import glob, fileinput
for filename in glob('*' + ext):

    f_in = (x.rstrip() for x in open(filename, 'rb').readlines()) #instead of trying to modify in place we instead read in data and replace raw_values.
    with open(filename, 'wb') as f_out: # we then write the data stream back out     
    #extra modification to the data can go here, i just remove the /r and /n and write back out
        for i in f_in:
            f_out.write(i)

    #now there is no need to read the data back in because we already have a static referance to it.

答案 1 :(得分:1)

对我来说,它适用于此代码:

#!/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)

注意我如何迭代这些线!