I have following script properly identifies ASCII and non-ASCII lines, but I want a report for each file, not per line. Since I have the print inside the loop, and I have many files, I get far too much output. How can I modify this code to get a single output per file? It should tell me whether there was any non-ASCII text in the file.
import os
for file in os.listdir('.'):
if file.endswith('.txt'):
with open(file) as f:
content = f.readlines()
for entry in content:
try:
entry.encode('ascii')
except UnicodeEncodeError:
print("it was not a ascii-encoded unicode string")
print(file)
else:
print("It may have been an ascii-encoded unicode string")
print(file)
with open(file) ...context manager but inside thefor file in ...block