'Find Binary in a dir of text files Python or Bash
I have a series of log files that are all text.
If a certain error happens it results in binary being inserted in the text.
I want a Python or Bash script that will:
- Go through the text files (logs) in the dir
- Show the line that includes the binary
- Give me the line number where it occurs as well as the file name
Solution 1:[1]
Here's what worked for me in Python.
import os
import sys
def main():
if len(sys.argv) < 2:
print('Usage: python3 hasbinary.py <DIR>')
return -1
DIR = sys.argv[1]
for file in os.listdir(DIR):
msgs = []
if file.endswith('.log'):
print(f'Reading file {file}.')
with open(DIR + '/' + file, 'rb') as f:
msgs = msgs + f.readlines()
for msg in msgs:
try:
msg.decode()
except:
print(f'Contains binary data: {msg}')
if __name__ == '__main__':
main()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | addzo |
