Looking for tabs in file

#tested it some more and it does not work.
from sys import argv

script, filename = argv
count = 0
try:
    txt = open(filename).read()
    #count = txt.count('\t')
    print("try statement executed.")
    for line in txt:
        if '\t' in line:
            count += 1
    print(f"{count}Tab characters found.")
    #filename.close()
except FileNotFoundError:
    print(f"file not found {filename} ")

Nice, that works. I fixed your post by putting [code] … [/code] around your code so that it formats right. Python doesn’t survive the default formatting.

Now, check this out:

import sys
assert '\t' not in open(sys.argv[1]).read(), "You've got tabs."

Your’s is much more useful, but that 2 line script does it too.

Also, you’re almost able to print out the lines that have the errors, with line numbers. Change the read() to readlines() and you’ll get each line from the file as a list.

1 Like