Python 文件的读写操作
2012-01-15 16:44
531 查看
Python 文件的读写操作:
#!/usr/bin/python
#Filename:using_file.py
poem='python'
f=file('C:\\poem.txt','w') # open for 'w'riting.It will create new file
f.write(poem)#write text to file
f.close()#close the file
f=file('C:\\poem.txt','a') # open for 'a' .It will add string at old file
f.write(' very funning')
f.close()
f=file('C:\\poem.txt')
#if no mode is specified,'r'ead mode is assumed by default
while True:
line=f.readline()
if len(line)==0:
break
print line
f.close()#close the file
程序解读:
不管C盘存不存在,都创建poem.txt文本,写入一些文字后。再以追加的方式打开(不是重新创建),加入文字。最后以只读的方式读取文件
#!/usr/bin/python
#Filename:using_file.py
poem='python'
f=file('C:\\poem.txt','w') # open for 'w'riting.It will create new file
f.write(poem)#write text to file
f.close()#close the file
f=file('C:\\poem.txt','a') # open for 'a' .It will add string at old file
f.write(' very funning')
f.close()
f=file('C:\\poem.txt')
#if no mode is specified,'r'ead mode is assumed by default
while True:
line=f.readline()
if len(line)==0:
break
print line
f.close()#close the file
程序解读:
不管C盘存不存在,都创建poem.txt文本,写入一些文字后。再以追加的方式打开(不是重新创建),加入文字。最后以只读的方式读取文件
相关文章推荐
- python文件的读写操作
- python中 对文件的读写操作 以及如何边写入 边保存flush()
- Python文件操作读写文件
- python文件读写并使用mysql批量插入示例分享(python操作mysql)
- python基础5--文件操作--字符串读写
- python里面对文件的读写操作
- Python中的文件IO操作(读写文件、追加文件)
- Python文件操作读写文件
- python对excel文件的读写操作
- python使用codecs模块进行文件操作-读写中英文字符
- Python 文件读写操作实例详解
- Python 文件读写操作实例详解
- python文件读写操作与linux shell变量命令交互执行
- python文件读写操作与linux shell变量命令交互执行的方法
- python读写操作csv文件
- Python基础操作 文件的读写 异常模块 类面向对象 时间日历
- python---实战日志文件中查找特定字段,文件操作读写、排序、替换、列表、集合
- python基础6--文件操作--对象的读写
- 快速入门Python中文件读写IO是如何来操作外部数据的?
- Python 文件读写操作实例详解