-1
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.

John Kugelman
365k70 gold badges555 silver badges600 bronze badges
asked Apr 15, 2011 at 20:40
1
  • 1
    This error is saying that python doesn't know why the line with the if float is 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. Commented Apr 15, 2011 at 20:55

3 Answers 3

5

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
answered Apr 15, 2011 at 20:44
Sign up to request clarification or add additional context in comments.

3 Comments

While this is one of the OP's problems, it's not the one he asked the question about, so it doesn't answer his question. You don't mention his indentation error at all.
The missing 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.
I realize that but there's another error that you don't talk about. According to my interpreters (v2&v3) your code throws the same error that his did. This other error is being caused by his if float(RAM) > 4096: line having an indentation level too small to be in the function and too large to be out of it.
1

You must follow the try block with a catch or finally block at the same tab level

answered Apr 15, 2011 at 20:44

2 Comments

Can you please give me example?
What should i put after f.close()?
0

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?")
answered Apr 15, 2011 at 20:55

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.