When I copy your code to a file, test.py, and run
cat -A test.py
I see
//How it appears in my editor$
def SaveList(self, directory):$
templist = []$
templistbox2 = []$
for n,i in enumerate(self.listbox2.get(0,END)): $
templistbox2.insert(n, re.sub(r'^[0-9]*[.]',"",str(i)))$
for filename in sorted(os.listdir(directory)):$
self.templist.insert(i, filename)$
^I print filename #whitespace error here$
which indicates there is a tab (represented by ^I
) followed by four spaces on the last line.
I'm not sure what the equivalent tool on Windows would be, but the Mac should have the cat -A
command. It will show you where the tabs versus spaces are.
There is a program called reindent.py which will convert tabs to spaces for you:
reindent.py test.py
On Unix there is also a unexpand command which converts spaces to tabs.
Most Python programmers use spaces rather than tabs for indentation. Most of the Python code you find on the web will use spaces rather than tabs.
Your editor may be adding tabs, but if you took a snippet of code from the web, your file may now contain both tabs and spaces.
It is easiest to go with the flow and adopt the spaces-as-indentation convention, so you will not have to reformat other people's code so much.
By the way, adopting the spaces-as-indentation convention does not mean having to press SPACE 4 times for each indentation level. Your editor should have a configuration option which makes pressing TAB insert 4 spaces.