我正在尝试使用NamedTempopraryFile创建一个临时文件数组,但它似乎不像我想要的那样工作。只创建一个临时文件并返回函数,而不是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
答案 0 :(得分:1)
在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