-2

I have a folder with nearly 150 *.txt files. I need to delete the 1st seven lines of every .txt file in that folder using python3.5.2

slash
  • 3
  • 1

1 Answers1

0

You can use Python's OS Module to get the names of all *.txt files from your folder. Then you can iterate over this names, read all lines from each file and overwrite the file with the lines you want to keep:

from os import listdir, path

path_str = '.'  # your directory path
txts = [f for f in listdir(path_str)
        if f.endswith('.txt') and path.isfile(path.join(path_str, f))]

for txt in txts:
    with open(txt, 'r') as f:
        lines = f.readlines()

    with open(txt, 'w') as f:
        f.write(''.join(lines[7:]))
Aurora Wang
  • 1,822
  • 14
  • 22