RAM=open('/root/arg2', 'r').read()
if RAM=="":
try:
if not sys.argv[2]=="":
f = open("/root/arg2","a")
f.write(sys.argv[2])
f.close()
if float(RAM) > 4096:
os.system("echo What the hell?")
What's wrong with the script above? The line after f.close() always gives error when compiling. Error from compiling:
riki137@riki137-K70IC:~/scripts$ python -m compileall *
Compiling thatscript.py ...
Sorry: IndentationError: ('unexpected unindent', ('thatscript.py', 20, 1, '\tif float(RAM) > 4096:\n'))
I have tried spaces, different commands, new blank lines. None of that solved it.
3 Answers 3
You need to have an except or finally block for your try.
If you don't want to do anything in the event of an exception just put pass in it.
try:
if not sys.argv[2]=="":
f = open("/root/arg2","a")
f.write(sys.argv[2])
f.close()
except:
pass
Failing to have this block can cause the IndentationError you're experiencing:
try:
foo = 1
bar = 2
baz = 3
File "<pyshell#13>", line 5
baz = 3
^
IndentationError: unindent does not match any outer indentation level
3 Comments
except/finally block is the indentation error. You can't just dedent a try block without having a corresponding except/finally block or it will throw this error.if float(RAM) > 4096: line having an indentation level too small to be in the function and too large to be out of it.You must follow the try block with a catch or finally block at the same tab level
Remove the try: statement, as it's not doing anything.
RAM=open('/root/arg2', 'r').read()
if RAM=="":
if not sys.argv[2]=="":
f = open("/root/arg2","a")
f.write(sys.argv[2])
f.close()
Others have stated this too, but I'd be wary of just using an empty except: block, as this could lead you to believe things had been done that had infact failed.
Also, your next statement will fail, as if RAM=="" then float(RAM) will raise a ValueError. This might be an indentation problem with your question, but it should probably look something like:
if RAM=="":
#Your sys.argv bit here
elif float(RAM) > 4096:
os.system("echo What the hell?")
if floatis only indented 4 spaces. It is like having an extra close brace in other languages. Place the if: block at same indentation level as the try: and you will see a new error that will help you along the way.