我有file1.py
:
def battery():
pass
// much more methods here
if __name__ == "__main__":
cmd = str((sys.argv)[1]) + "()"
os.system(cmd)
现在我想用file1.battery()
从linux控制台调用python file1.py battery
。
但我收到错误:
sh: 1: Syntax error: end of file unexpected
答案 0 :(得分:1)
您可以使用eval编译字符串代码或使用全局和本地:
import sys
current_module = sys.modules[__name__]
function = getattr(current_module, 'func')
print function()
此外,模块可以自行导入:
with open("media/test.mp3", "rb") as f:
AudioSegment.from_mp3(f)