今天继续码~python的文件访问~
来源:互联网 发布:js移除元素的属性值 编辑:程序博客网 时间:2024/06/07 06:50
为什么...昨天保存的博客不见了...难道是审核未通过嘛?也不告诉我...
进入正题吧~
首先是文件的写入:
诶,重复写昨天的东西,好烦好烦好烦。。。
import os#不同系统下的换行符ls = os.linesep#要求输入文件名,并检查文件是否重名while True: fname = raw_input("Hi,let me know the name of the New file:") if os.path.exists(fname): print "ERROR,'%s' already exists!Please name the file again~" % fname else: breakall = []print "\n Enter lines ('.' by itself to quit).\n"#输入文件内的每一行while True: entry = raw_input('>> ') if entry == '.': break else: all.append(entry)#写入文件,并关闭文件fobj = open(fname, 'w')fobj.writelines(["%s%s" % (x,ls) for x in all])fobj.close()print "Done!Tada!!"上述代码的输出结果为:
>>> Hi,let me know the name of the New file:spy Enter lines ('.' by itself to quit).>> 12345>> 67890>> .Done!Tada!!>>>
filename = raw_input("Enter the filename that you want to read:")printtry: fobj2 = open(filename, 'r')except IOError,e: print "***Bibibi,Sorry, file open ERROR:",eelse: for eachLine in fobj2: print ">> ",eachLine, fobj2.close() print "DONE!Tada!!"
运行结果:
>>> Enter the filename that you want to read:test.py>> str1 = "abcdef">> print str1[::-1]>> print str1[::-2]>> list1 = [123, "xyz",123.123,'abc']>> print list1[::-1]>> >> print id(str1)>> print id(list1)>> DONE!Tada!!>>>
0 0
- 今天继续码~python的文件访问~
- 今天继续学习winform遇到的东东
- python \的作用是继续
- 继续探讨javascript的跨域访问
- 今天继续通宵
- 今天继续会议设计
- 今天继续不上班
- 今天继续看Mule
- 今天继续郁闷
- 今天继续看代码
- 昨天聚会,今天继续。
- 今天继续看不懂
- 今天继续学习symfony
- 今天继续水题
- python访问csv文件
- 继续翻译今天的暴雪官网新闻
- 20120907-20130420罪过的虚度,今天星期一,继续行走
- 今天继续学习锋利的JQuery,第三章后半段
- 一个计算HBase表中行数的MapReduce程序
- 黑马程序员——基础知识——数组
- 传统轮播
- POJ 3074 解题报告
- 关于显著性检测的思考
- 今天继续码~python的文件访问~
- Servlet学习
- 《机器学习实战》笔记之九——树回归
- 数据结构实践项目——队列
- 不要学习代码,要学会思考
- *LeetCode-Find the Duplicate Number
- 揭秘利用互联网营销的全国拍卖骗子公司 - (深圳拍卖公司)
- 一秒解决直接双击启动tomcat-startup.bat闪退
- ZXV10 B700V5机顶盒支持的无线网卡主芯片