Created
July 13, 2022 00:42
-
-
Save shollingsworth/47bada592a273c7a447ab3dc7f31418b to your computer and use it in GitHub Desktop.
Find common strings in multiple files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| """Find common strings in multiple files.""" | |
| import argparse | |
| from io import BytesIO | |
| from pathlib import Path | |
| import tokenize | |
| def _itertokens(file: Path): | |
| tokens = tokenize.tokenize(BytesIO(file.read_bytes()).readline) | |
| for token in tokens: | |
| yield token.string | |
| def run(args): | |
| fdict = {} | |
| for file in args.files: | |
| z = Path(file) | |
| fdict[z] = set(_itertokens(z)) | |
| setresult = set.intersection(*fdict.values()) | |
| for i in sorted(setresult): | |
| print(i) | |
| def main(): | |
| """Run main function.""" | |
| parser = argparse.ArgumentParser( | |
| formatter_class=argparse.RawDescriptionHelpFormatter, | |
| description=__doc__, | |
| ) | |
| parser.add_argument( | |
| "files", | |
| nargs="+", | |
| help="Files to use", | |
| ) | |
| args = parser.parse_args() | |
| run(args) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment