Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from collections import Counter
- # Will grab all txt files in directory you are in when running the script
- # Then it will go over them and count the lines in all the files that are the same
- # Lines that are common across all files will be deleted
- dir_path = os.getcwd()
- files = [f for f in os.listdir(dir_path) if f.endswith('.txt')]
- unique_lines = set()
- for file in files:
- with open(os.path.join(dir_path, file), 'r') as f:
- lines = [line.strip() for line in f.readlines()]
- unique_lines.update(lines)
- line_counts = Counter()
- for file in files:
- with open(os.path.join(dir_path, file), 'r') as f:
- lines = [line.strip() for line in f.readlines()]
- line_counts.update(lines)
- common_lines = [line for line, count in line_counts.items() if count == len(files)]
- for file in files:
- with open(os.path.join(dir_path, file), 'r+') as f:
- lines = [line.strip() for line in f.readlines()]
- new_lines = [line for line in lines if line not in common_lines]
- f.seek(0)
- f.write('\n'.join(new_lines))
- f.truncate()
- print("Docs cleaned of common info.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement