Python execfile() 函數
描述
execfile() 函數可以用來執行一個檔。
語法
以下是 execfile() 方法的語法:
execfile(filename[, globals[, locals]])
參數
- filename -- 檔案名。
- globals -- 變數作用域,全局命名空間,如果被提供,則必須是一個字典對象。
- locals -- 變數作用域,局部命名空間,如果被提供,可以是任何映射對象。
返回值
返回運算式執行結果。
實例
以下展示了使用 execfile 函數的實例:
假設檔 hello.py,內容如下:
print('zaixian');
execfile 調用該檔
>>>execfile('hello.py')
zaixian