我刚开始学习python。我正在使用 python 2.7.5 我创建了名为“t.py”的测试模块,如下所示:
$cat > t.py
import os
def greeting(name):
print("Hello, " + name)
c=os.system('clear')
当我尝试在 python 中使用该模块时,c 无法正常工作,但问候语可以正常工作
$python
>>> t.greeting("test")
Hello, test
>>> t.c
0
能否请您告知为什么 t.c 没有在 python 中清除屏幕。谢谢。
请您参考如下方法:
因为您已将 c 设置为变量(一次调用 os.system('clear') 的结果),而不是函数。你必须做
def c():
os.system('clear')
然后调用
>>> t.c()
仅供引用,subprocess.call 优于 os.system,后者仅用于向后兼容。
