I’m currently using Komodo Edit in Windows 7, however, I have experienced this problem on my Mac with TextWrangler. For whatever reason I’m getting some kind of whitespace error which is a huge problem when I’m writing in Python. For example, everything appears to be properly tabbed, but Komodo is currently giving me an “Ambiguous whitespace” error
//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)
print filename #whitespace error here
Considering I’ve experienced this with two different editors on both windows and Mac, I’m wondering if there’s some setting I don’t know about, or if I’m doing something wrong.
When I copy your code to a file, test.py, and run
I see
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 -Acommand. 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:
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.