在 python 中创建临时文件数组

Create an array of temp files in python

我正在尝试使用 NamedTempopraryFile 创建一个临时文件数组,但它似乎没有按我预期的方式工作。只创建了一个临时文件和函数 returns,而不是 db_file_paths.

中的许多元素

有没有人看错了?

示例输入:db_file_path = ["/mnt/somedir/data_0" , /mnt/somedir/data_1"]

def create_ini(db_file_paths):

    #array of file tmpfs ini filehandles
    file_handles = []

    for path in db_file_paths:
        #make a tempfs file handle
        f_handle = tempfile.NamedTemporaryFile(delete=False)
        #match and replace db dummy file path
        f_handle = replace( f_handle.name, source_ini, pattern, path )
        file_handles.append(f_handle)
        return file_handles

您 return 在 for 循环的第一次迭代结束时。重新缩进最后一行,以便仅在 for 循环结束后执行。

def create_ini(db_file_paths):

    #array of file tmpfs ini filehandles
    file_handles = []

    for path in db_file_paths:
        #make a tempfs file handle
        f_handle = tempfile.NamedTemporaryFile(delete=False)
        #match and replace db dummy file path
        f_handle = replace( f_handle.name, source_ini, pattern, path )
        file_handles.append(f_handle)
    return file_handles # <-- change of indentation here