引言:

  最近大半年都在学习python编程,在双十一的时候购买了《Python编程核心》,看到makeTextFile.py和readTextFile.py两个例子有点错误,所以在这里给修正一下!

makeTextFile.py脚本:

#!/usr/bin/env python
#_*_coding:utf8_*_ 'makeTextFile.py -- create text file' import os ls = os.linesep #get filename
while True:
#需要添加的语句,并且需要缩进,后面的四条语句也需要缩进
fname = raw_input("please input file name:\n")
if os.path.exists(fname):
print "ERROR: '%s' already exists" % fname
else:
break
#get file content (text) Lines
all_list = []
"""原著上使用了all做list,但是在使用eclipse是发现有assignment to reserved built-in symbol:all的warning,所以就使用all_list"""
#all = []
print "\nEnter lines ('.' by itself to quit).\n" #loop until user terminates input
while True:
entry = raw_input('> ')
if entry == '.':
break
else:
all_list.append(entry)
#write lines to file with proper line-ending
fobj = open(fname,'w')
fobj.writelines(['%s%s' % (x,ls) for x in all_list])
fobj.close()
print 'DONE!'

readTextFile.py脚本:

#!/usr/bin/env python
#_*_coding:utf8_*_ 'readTextFile.py -- read and display text file'
#from makeTextFile import fobj #get filename
fname = raw_input('Enter filename: ')
print #attempt to open file for reading
try:
fobj = open(fname,'r')
except IOError,e:
print "*** file open error: ",e
else:
#display contents to the screen
for eachLine in fobj:
print eachLine,
fobj.close()

Python编程核心之makeTextFile.py和readTextFile.py的更多相关文章

  1. Python 编程核心知识体系(REF)

    Python 编程核心知识体系: https://woaielf.github.io/2017/06/13/python3-all/ https://woaielf.github.io/page2/

  2. Python编程核心内容之一——Function(函数)

    Python版本:3.6.2  操作系统:Windows  作者:SmallWZQ 截至上篇随笔<Python数据结构之四--set(集合)>,Python基础知识也介绍好了.接下来准备干 ...

  3. Python编程核心内容 ---- Function(函数)

    Python版本:3.6.2  操作系统:Windows  作者:SmallWZQ 截至上篇随笔<Python数据结构之四——set(集合)>,Python基础知识也介绍好了.接下来准备干 ...

  4. Python编程核心内容之二——切片、迭代和列表生成式

    Python版本:3.6.2  操作系统:Windows  作者:SmallWZQ 最近太忙啦.很多事情需要自己处理,感觉时间不够用啊~~~~今后,博客更新时间可能会慢下来,哈哈,正所谓"人 ...

  5. Python编程核心内容 ---- 切片、迭代和列表生成式

    Python版本:3.6.2  操作系统:Windows  作者:SmallWZQ 最近太忙啦.很多事情需要自己处理,感觉时间不够用啊~~~~今后,博客更新时间可能会慢下来(但不能荒废了学习,要学习就 ...

  6. Python 编程核心知识体系-模块|面向对象编程(三)

    模块 面向对象编程

  7. Python 编程核心知识体系-基础|数据类型|控制流(一)

    Python知识体系思维导图: 基础知识 数据类型 1.序列 2.字符串 3.列表和元组 4.字典和集合 循环 & 判断

  8. Python 编程核心知识体系-文件对象|错误处理(四)

    错误处理 文件对象

  9. Python 编程核心知识体系-函数(二)

    函数

随机推荐

  1. SwipeRefreshLayout实现上拉加载

    原来的Android SDK中并没有下拉刷新组件,但是这个组件确实绝大多数APP必备的一个部件.好在google在v4包中出了一个SwipeRefreshLayout,但是这个组件只支持下拉刷新,不支 ...

  2. Java多线程 wait, notify 和 notifyAll

    Java的Object类 public class Object { public final native void notify(); public final native void notif ...

  3. uiimageView连续帧动画

    // //  MJViewController.m //  05-汤姆猫 // //  Created by apple on 14-3-24. //  Copyright (c) 2014年 itc ...

  4. 重写NSLog,Debug模式下打印日志和当前行数

    在pch文件中加入以下命令,NSLog在真机测试中就不会打印了 //重写NSLog,Debug模式下打印日志和当前行数 #if DEBUG #define NSLog(FORMAT, ...) fpr ...

  5. 在xocde运行profile 遇到"Existing default base temp directory '/Library/Caches/com.apple.dt.instruments' has insufficient privileges for user id 505. Please have the owner delete this directory"

    找到这篇文章 http://stackoverflow.com/questions/34153795/xcode-7-unable-to-open-instruments-from-developer ...

  6. IOS推荐学习网站

    1> 个人博客:技术大牛 唐巧:http://blog.devtang.com/blog/archives/ 王巍:http://www.onevcat.com 破船之家:http://beyo ...

  7. OLTP与OLAP的差异

    OLTP与OLAP的差异 系统类型 OLTP(在线交易系统) OLAP(联机分析系统),DW(数据仓库) 数据来源 操作数据,OLTP通常是原始性数据源 联合型数据:OLAP数据来源于其他OLTP系统 ...

  8. Lintcode: Kth Smallest Number in Sorted Matrix

    Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ ...

  9. 树形DP(01组合背包The Ghost Blows Light HDU4276)

    题意:有n个房间,之间用n-1条道路连接,每个房间都有一个定时炸弹,在T时间后会一起爆炸,第i个房间有pi价值的珠宝,经过每条道路都需要花费一定的时间,一个人从1房间开始 ,从n房间出去,保证再不炸死 ...

  10. UML: 序列图

    摘自http://www.umlonline.org/school/thread-37-1-1.html 大家都进过餐馆吃饭吧?你是如何和餐厅服务员“眉来眼去”的呢?回忆一下从你进餐馆开始到你离开餐馆 ...