What's wrong with this python script?
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.
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
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?")
精彩评论