0

So I've been set the task to load a file for an encryption program I'm working on, but I'm getting an error stating that the console has came across an Unexpected unindent, yet I have no idea where it is! I've assumed it is a glitch, tried rewriting the code, yet still I've been getting the same error! I'm an amateur coder, can anyone please help me out?

This is part of the code I'm having trouble with:

def loadFile():
    try:
        aFile = input('Plese enter a filename: \n')
        myFile = open(aFile, 'r+')
        global myFile
        try:
            print('Gathering file information...')
            time.sleep(1)
            print('The string you have chosen to encrypt is:')
            print(myFile.read())
        finally:
            myFile.close()
    except IOError:
        print('The file does not exist \n')
        loadFile()
Marcello B.
  • 4,177
  • 11
  • 45
  • 65
Python
  • 3
  • 1
  • 4
    The traceback *tells* you where this occurs. You are probably mixing tabs and spaces; force your editor to only use spaces (expand tabs). – Martijn Pieters Sep 19 '15 at 16:42
  • 1
    Python tip: use `with open(aFile, 'r+') as myFile:` so you don't have to use `try...finally`. Also, why make `myFile` a global here? You close it within the function. Recursion is not a good way to handle the `IOError` case, use a `while` loop instead, because you'll run out of recursion stack if the file is never created. – Martijn Pieters Sep 19 '15 at 16:44
  • I made myFile a global variable as I was going to be accessing it outside of the function to read the user's file. This is only a snippet of my program, as this is the only point I'm having major problems with. And we where taught to use recursion in our error catching as that would return the user to the start. When I was testing my program before, I had no problems with the IOError, and a couple days before this program was working fine with no unexpected unindents or anything. I didn't make any amends to this section and it just stopped working. That's what's troubled me... – Python Sep 20 '15 at 15:38
  • That you are being taught to use recursion for this is.. troubling. You are building up a recursion stack here with no benefit, and huge downsides (memory is not freed until the recursive calls return, for example). You are closing the file object, so setting it as a global is pointless, you cannot read from a closed file. – Martijn Pieters Sep 20 '15 at 15:41
  • Sorry about this, I'm rubbish at coding really (regretting taking it as a subject now). So how can I use a while loop to fix this? – Python Sep 20 '15 at 15:49
  • An infinite loop: `while True: ...` would continue until you exit it with `return` or `break` (returning from a function or continuing with the code after the `while` loop). – Martijn Pieters Sep 20 '15 at 15:51
  • Also see [Asking the user for input until they give a valid response](http://stackoverflow.com/q/23294658) where a loop is used to ask a user for valid input. – Martijn Pieters Sep 20 '15 at 15:52
  • Alright, thanks for the help, I'd never have seen that before :) – Python Sep 20 '15 at 15:53
  • Note that *if* there is an `IOError` exception opening the file, you're likely to hit the recursion limit in a fraction of a second as you will try to open the file 1000 times without delays in between. An infinite loop would just endlessly try again and again. – Martijn Pieters Sep 20 '15 at 15:56
  • UPDATE: The problem has been solved! It turns out that all it took was to rewrite it in another program! No need to convert tabs, spaces, anything! Funny enough I only realize this as I'm handing the broken code to my teacher...fml XD – Python Sep 21 '15 at 18:23

1 Answers1

0

you can use this command -

perl -i.bak -pe "s/\t/' 'x(8-pos()%8)/eg" file.py

here file.py is your file name. 8 is the tab indent which you would have configured in your editor. Basically this error comes up with mismatch between tabs and spaces. So this command will convert all tabs with spaces. Hope this works :)

dragster
  • 448
  • 1
  • 4
  • 20