在python下file类型、open方法
来源:互联网 发布:如何下载英语翻译软件 编辑:程序博客网 时间:2024/05/17 02:45
python中file和open有什么区别?
file 估计是一种类型, 代表一个文件, 而open 是一个方法,代表一个动作。打开文件的动作,相应的应该还有close, 关闭文件。
一、用Python创建一个新文件,内容是从0到9的整数, 每个数字占一行:
#python>>>f=open('f.txt','w') # r只读,w可写,a追加
>>>for i in range(0,10):f.write(str(i)+'\n')
. . .
>>> f.close()
二、文件内容追加,从0到9的10个随机整数:
#python
>>>import random
>>>f=open('f.txt','a')
>>>for i in range(0,10):f.write(str(random.randint(0,9)))
. . .
>>>f.write('\n')
>>>f.close()
三、文件内容追加,从0到9的随机整数, 10个数字一行,共10行:
#python
>>> import random
>>> f=open('f.txt','a')
>>> for i in range(0,10):
. . . for i in range(0,10):f.write(str(random.randint(0,9)))
. . . f.write('\n')
. . .
>>> f.close()
四、把标准输出定向到文件:
#python
>>> import sys
>>> sys.stdout = open("stdout.txt", "w")
>>> . . .
- 在python下file类型、open方法
- Python的file()和open()方法的区别
- 【python】file.write() 和 open()
- open file in python demo
- Python 之 open() 和file()
- python在提示符下使用open打开文件失败原因
- python os.open()方法
- windows下 no such file Python.h canot open pythonxx.lib解决方案
- Python 用file 对象和open方法处理文件的区别
- Spyder在Windows环境下出现RuntimeError: Could not open file ../caffe/deploy.prototxt
- Spyder在Windows环境下出现RuntimeError: Could not open file ../caffe/deploy.prototxt
- Python编程->cannot open file 'python27_d.lib'
- Python IOError: File not open for writing
- 在ubuntu下安装matlab:libXp.so.6: cannot open shared object file: No such file or directory
- 初探python中的file类型
- python: cannot open shared object file: No such file
- Python file.truncate()方法
- Python File(文件) 方法
- 2011-8-1 21:13:48
- 自己的无线网卡使用--方便以后自己查看
- 【VC++】VS2005中MessageBox问题
- 做Web Application开发我常去的网站
- 实习归来
- 在python下file类型、open方法
- 小SQL(一)
- 【VC++】VC对话框中增加菜单栏
- 地面搜索(上) (2008高教社杯全国大学生数学建模竞赛)
- Interlocked系列互锁函数
- googlemaps API V3 之(四)marker局部加载优化处理
- iptables限制连接数量三种限制方式
- mysql 的root密码忘了的重置方法
- 【VC++】在对话框中使用ON_UPDATE_COMMAND_UI更新菜单