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?")
Run Code Online (Sandbox Code Playgroud)
上面的脚本有什么问题?f.close()之后的行总是在编译时出错.编译时出错:
riki137@riki137-K70IC:~/scripts$ python -m compileall *
Compiling thatscript.py ...
Sorry: IndentationError: ('unexpected unindent', ('thatscript.py', 20, 1, '\tif float(RAM) > 4096:\n'))
Run Code Online (Sandbox Code Playgroud)
我尝试过空格,不同的命令,新的空行.这些都没有解决它.
你需要有一个except或一个finally块try.
如果您只是在插入异常时不想做任何事情pass.
try:
if not sys.argv[2]=="":
f = open("/root/arg2","a")
f.write(sys.argv[2])
f.close()
except:
pass
Run Code Online (Sandbox Code Playgroud)
没有这个块可能会导致IndentationError你遇到:
try:
foo = 1
bar = 2
baz = 3
File "<pyshell#13>", line 5
baz = 3
^
IndentationError: unindent does not match any outer indentation level
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
151 次 |
| 最近记录: |