-3

the following code is returning some strange indentation error inside a for statement. The console is pointing the error at a list.append() method. It should be a silly misstake onetime I am new at Python. heh

import os
import sys
import re
import glob

from Text import Text
from Word import Word
from FileCategory import FileCategory

class FileIO(object):

    def loadFile(self, filePath):

        newText = Text(os.path.basename(filePath))

        words=[]
        with open(filePath) as buff:
            content = buff.read().lower()
            re.sub("[^\w\s]", "", content)
            re.sub("[0-9]", "", content)
            words = content.Split(' ')

        for word in words:
            wordFound = next(auxWord for auxWord in newText.words if auxWord.textWord == word) 
            if wordFound is None:
                newWord = Word(word, 1)
                newText.words.append(newWord)
            else:
                wordFound.countInText+=1

        return newText

    def loadFilesFromDirectory(self, path):
        newCategory = FileCategory()
        files=[]
        os.chdir(path)
        for file in glob.glob("*.txt"): 
            files.append(file)

        for filePath in files:
            newFile = loadFile(filePath)
            if newFile is not None:
                newCategory.files.append(newFile)

        return newCategory

Log:

Traceback (most recent call last):
  File "aprendizadMaq.py", line 6, in <module>
    from FileIO import FileIO
  File "/home/adolfosrs/Dropbox/7º Semestre/IA/T2/pyAprendizadMaq/FileIO.py", line 38
    files.append(file)
        ^
IndentationError: expected an indented block

Any Idea?

adolfosrs
  • 9,286
  • 5
  • 39
  • 67

2 Answers2

4

The problem arises from the fact that python doesn't like it when you mix tabs and spaces for indentation. Python relies on whitespace a LOT to figure out the levels of indentation, and hence scope.

As a result, when you have a line that has two TABs on it, followed by a line with 4 SPACEs and a TAB, python proceeds to freak out and tell you that your indentation is off.

TL;DR: don't mix tabs and spaces. Stick to one or the other, and you should be fine.

Fun aside: if you really /really/ REALLY wanted to ignore this tip and try to mix the two, I believe TABs are considered to be the equivalent of 8 SPACEs. So you could in theory do some indentation math there, to figure out how much to indent each line by, with a mix of the two. But this is HIGHLY unrecommended, and I'm probably going to burn in hell just for saying it

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
1

Open your file in a text editor that allows you to view whitespace. Chances are you're mixing tabs and spaces.

bstar55
  • 3,542
  • 3
  • 20
  • 24
  • Does Sublime has this feature? – adolfosrs May 30 '14 at 04:27
  • 1
    @adolfosrs - Yes, it does. I have `"draw_white_space": "selection"` in my user preferences, so I can select text and see what kind of white space is present. Spaces are indicated by dots, and tabs by long dashes. – MattDMo May 30 '14 at 04:29
  • http://stackoverflow.com/questions/10153998/sublime-text-2-view-whitespace-characters – bstar55 May 30 '14 at 04:29