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
            Asked
            
        
        
            Active
            
        
            Viewed 47 times
        
    -2
            
            
        - 
                    Python seems to be a wrong tool for the task – mustaccio Dec 05 '18 at 20:18
 - 
                    @JackMoody I have many files, not one file – slash Dec 05 '18 at 20:26
 - 
                    @slash Use a loop. – blhsing Dec 05 '18 at 20:27
 - 
                    What does your folder structure look like? You need to loop through each file and then do what is mentioned in the link I’ve given. – Jack Moody Dec 05 '18 at 20:27
 - 
                    All the files are structured like this: Name: Source: RPS: LIMP: CULP: MCL: DVS: 34 345 2321 232 4564 3245 9098 I want to delete lines from 0 to 6. i am trying to use os.walk. here is my code: – slash Dec 05 '18 at 20:45
 
1 Answers
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