import os directory = os.listdir("E:\append_some_line_to_the_start_of_a_file") for file in directory: print(file)
afer this I want to write in all the files in the same time.
import os directory = os.listdir("E:\append_some_line_to_the_start_of_a_file") for file in directory: print(file)
afer this I want to write in all the files in the same time.
You should use ExitStack for this:
from contextlib import ExitStack
import os
dir = 'E:\append_some_line_to_the_start_of_a_file'
with ExitStack() as stack:
files = [stack.enter_context(open(os.path.join(dir,fname), 'w')) for fname in os.listdir('./dir')]
for file in files:
file.write('test')