-3

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.

  • https://stackoverflow.com/questions/4617034/how-can-i-open-multiple-files-using-with-open-in-python This thread seems to be what you are looking for – DoubleRainbowZ Sep 16 '19 at 04:29

1 Answers1

0

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')
Lord Elrond
  • 13,430
  • 7
  • 40
  • 80