我my_code.py在Home/Python_Codes文件夹中有一个python文件()ubuntu.我想在python shell中运行它.我怎样才能做到这一点?
我做这个
>>> execfile('~/Python_Codes/my_code.py')
但它给了我路径错误
您应该将波浪号(〜)扩展为实际路径.请尝试以下代码.
在Python 2.x中:
import os
execfile(os.path.expanduser('~/Python_Codes/my_code.py'))
Run Code Online (Sandbox Code Playgroud)
在Python 3.x中(Python 3.x中没有execfile):
import os
with open(os.path.expanduser('~/Python_Codes/my_code.py')) as f:
exec(f.read())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5735 次 |
| 最近记录: |