python怎么打开文件
一、使用python源文件的几种方法\运行python脚本:
a.windows下打开shell(DOS提示符,命令行,cmd):
CMD命令进入某个目录
如在window下cmd运行python源文件xxx.py(注意这个xxx.py在C盘的python27目录下,若是其它盘的目录,就进入其它盘的目录来运行xxx.py)
python中打开文件可以使用后open(0函数,open()函数返回的是文件对象,对文件的操作都是在文件对象file上进行的。
打开cmd输入c:回车输入cdc:/python27/回车(ps:cd后面没有冒号!,如果需要在dos下查看带有空格的文件夹,要给文件夹加上引号如:CD"ProgramFiles"/PHP)输入pythonxxx.py或者xxx.py回车
这是在找到文件路径下去执行某文件,直接在cmd,python环境下输入pythonxxx.py会运行语法错误,不知是否是系统的环境变量没有添加好?
在linux下参见vamei:python基础
另附:cmd命令
1.进入上一层目录CD../
2.显示目录下的文件及了目录dir
b.Linux下运行python源文件:
$pythonxxx.py
c.在IDLE下运行python源文件
点击开始->程序->Python2.7->IDLE(PythonGUI)点击file->open->xxx.pyctrl+F5quit()是退出程序
d.在IDLE里,可以通过os执行系统命令,执行python源文件:
importos
os.system('pythonc:/xxx.py')
e.直接双击xxx.py
双击xxx.py,窗口一闪而过。很像VC运行时的Ctrl+F5对不对?那怎么办呢?(非windows系统可以跳过,不用此技巧)
这里我们在代码里加入一句话raw_input(),就可以。
二、利用Python的shell进行交互式开发:
这是Python的交互式的Shell,简称PythonShell,它能动态的,交互式地,显示对应代码程序的信息。
a.Python(commandline):这是命令行版本的PythonShell
开始快捷键+R,输入cmd,在命令行中输入python,直接输入python代码
或者快捷键+R,输入python,直接输入代码
或者开始菜单中找到python2.7下面的Python(commandline)
b.IDLE(PythonGUI):这是GUI,带图形界面的版本pythonshell
在开始菜单中找到python2.7下面的IDLE(PythonGUI),后直接输入代码。
以上内容为大家介绍了python培训之怎么打开文件,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python函数标注是什么
python函数标注是什么在参数的使用方法中,还有一种解包的情况是需要我们掌握的。比如,将列表或者字典的值转换为函数的参数,就需要用到参数解...详情>>
2023-11-14 14:13:35
python正负索引的使用
python正负索引的使用本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、说明使用正索引(从左至右)、负索引(从右至左)访问元素,时...详情>>
2023-11-14 13:45:04
python垃圾回收的机制过程
python垃圾回收的机制过程本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、步骤(1)收集所有容器对象(list,dict,tuple,customClas...详情>>
2023-11-14 13:05:15
pythontry语句的执行原理
pythontry语句的执行原理1、原理分析(1)执行try子句(try和except关键字之间的多行语句)。(2)如果没有异常发生,则跳过except子句并完成try语句...详情>>
2023-11-14 09:37:35热门推荐
技术干货






