在Python Shell中运行程序 [英] run program in Python shell

查看:110
本文介绍了在Python Shell中运行程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个演示文件:test.py. 在Windows控制台中,我可以使用以下命令运行文件:C:\>test.py

I have a demo file: test.py. In the Windows Console I can run the file with: C:\>test.py

我该如何在Python Shell中执行文件?

How can I execute the file in the Python Shell instead?

推荐答案

使用 execfile 用于 Python 2 :

>>> execfile('C:\\test.py')

对于 Python 3 使用 exec

>>> exec(open("C:\\test.py").read())

这篇关于在Python Shell中运行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆