python 简单的txt文件读写
1 读取txt文件。跟c相比,python的文件读写简直是方便的可怕
首先是读取文件
首先获得文件名称,然后通过 open函数打开文件,通过for循环逐行读出文件内容
#!python file by ninahao 10.30 'readfile.py--read and display text file' #get filename
fname=raw_input('enter file name:')
print #attempt to open file for reading
try:
fobj=open(fname,'r')
except IOError,e:
print 'open file--',fname,'--error!:',e
else:
#display content to the screen
for eachline in fobj:
print eachline
fobj.close()
2 写入文件并保存,同理,新建一个文件,也是通过open函数。神奇。
#!maketext.py
'maketextfile.py--create text file' import os
ls=os.linesep #get filename
while True:
fname=raw_input('enter file name : ')
if os.path.exists(fname):
print "ERROR: '%S' already exists" % fname
else:
break #get file content(text) lines
all=[]
print "\nEnter lines('.' by itself to quit).\n" #loop until user terminates input
while True:
entry=raw_input('>')
if entry=='.':
break;
all.append(entry)
##write lines to file with proper line-ending
fobj=open(fname,'w')
fobj.writelines(['%s%s' % (x,ls) for x in all])
fobj.close()
print 'DONE!'
python 简单的txt文件读写的更多相关文章
- python应用:TXT文件的读写
python读写TXT文件不需要导入包 python中常用的读写方式: 文件打开模式 描述 r 以只读模式打开文件,并将文件指针指向文件头:如果文件不存在会报错 w 以只写模式打开文件,并将文件指针指 ...
- Python之IO编程——文件读写、StringIO/BytesIO、操作文件和目录、序列化
IO编程 IO在计算机中指Input/Output,也就是输入和输出.由于程序和运行时数据是在内存中驻留,由CPU这个超快的计算核心来执行,涉及到数据交换的地方,通常是磁盘.网络等,就需要IO接口.从 ...
- 【Python】[IO编程]文件读写,StringIO和BytesIO,操作文件和目录,序列化
IO在计算机中指Input/Output,也就是输入和输出. 1.文件读写,1,读文件[使用Python内置函数,open,传入文件名标示符] >>> f = open('/User ...
- 转载-Python学习笔记之文件读写
Python 文件读写 Python内置了读写文件的函数,用法和C是兼容的.本节介绍内容大致有:文件的打开/关闭.文件对象.文件的读写等. 本章节仅示例介绍 TXT 类型文档的读写,也就是最基础的文件 ...
- python导入csv/txt文件
1. 导入csv文件 ### python导入csv文件的三种方法 ```python #原始的方式 lines = [line.split(',') for line in open('iris.c ...
- python新手学习之文件读写之修改
文件除r.w.a方式打开外,还可以有多种组合方式如r+ w+ a+等多种方式 1.r+ 读写模式介绍,开始读是从一行开始读,写永远从最后开始写(类似于追加) # f = open("test ...
- python笔记2 生成器 文件读写
生成器 一边循环一边计算的机制,称为生成器(Generator). 把一个列表生成式的[]改成(),就创建了一个generator: 创建了一个generator后,通过for循环来迭代它. 著名的斐 ...
- python中写入txt文件需要换行,以及\r 和\n
在Python中,用open()函数打开一个txt文件,写入一行数据之后需要一个换行 如果直接用 f.write(’\n’)只会在后面打印一个字符串’\n’,而不是换行’需要用 f.write(’\r ...
- [Python]-pandas模块-CSV文件读写
Pandas 即Python Data Analysis Library,是为了解决数据分析而创建的第三方工具,它不仅提供了丰富的数据模型,而且支持多种文件格式处理,包括CSV.HDF5.HTML 等 ...
随机推荐
- 使用iScroll时,input等不能输入内容的解决方法
做移动平台的应用,使用iscroll使屏幕上下滑动.发现当使用iscroll后,input等不能输入内容了.只要在iscroll.js文件中加入如下代码就ok了. function allowForm ...
- Aix下如何运行Java程序
windows下:java -classpath %classpath%;bb.jar;aa.jar [main class]main class是打包的主类,已经指定了主类,可以不输入.另外,IBM ...
- 【hadoop2.6.0】数据丢失问题解决
想自己走一遍从代码到运行的流程,结果各种错,郁闷啊. 问题① http://localhost:50070/ 里面一进去就告诉我块丢了.... 解决: bin/hadoop fsck -delete ...
- c#简易计算器
微软MSDN的代码库就有示例 http://code.msdn.microsoft.com/Simple-Calculator-54ec8e4a using System; using System. ...
- NodeVisitor的使用-遍历Geode节点并在它与父节点之间添加一个LOD节点
#include <osg\NodeVisitor>#include <osg\MatrixTransform>#include <osg\PagedLOD>#in ...
- a标签的妙用-拨打电话、发送短信、发送邮件
前端时间在做手机WAP网站时,遇到需要点击页面上显示的电话号能直接拨号的需求,查找资料发现可以使用html的a标签完美实现该需求!记录下来以备后用...... 目前主流手机浏览器对H5的支持已经很不错 ...
- [Android Pro] Android 进程级别 和 oom_adj对应关系
一 : 前台进程 (Active Process): oom_adj为0. 前台进程包括 : 1 : 活动 正在前台接收用户输入 2:活动.服务与广播接收器正在执行一个onReceive事件的处理函数 ...
- September 30th 2016 Week 40th Friday
Elegance is the only beauty that never fades. 优雅是唯一不会褪色的美. Even the most beautiful apperance may los ...
- iOS 开发多线程篇—GCD的常见用法
iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) ...
- 中文和unicode互转
public class Test { public static void main(String[] args) { String uname="欧阳红"; for (int ...