from sys import argv
script,filename=argv
#固定模式啦
print("we're going to erase %r."%filename)
#打印 we're going to erase “filename”
print("if you don't want that,hit CTRL-C(^C).")
#打印if you don't want that,hit CTRL-C(^C).
print("if you do want that,hit RETURN.")
#打印if you do want that,hit RETURN.
input("?")
#提示符:?
print("opening the file ...")
#打印opening the file ...
target=open(filename,'w+')
#以写的模式打开“filename”并打印,再赋值给target
print("truncating this file.Goodbye!")
#打印truncating this file.Goodbye!
target.truncate()
#对target文件进行清空(truncate)
print("now i'm going to ask you for three lines.")
#打印:now i'm going to ask you for three lines.
line1=input("line1:")
line2=input("line2:")
line3=input("line3:")
###输入line1/2/3的内容
print("i'm going to write these to the file.")
#打印i'm going to write these to the file.
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
#写入内容而且进行换行处理
print("and finally,we close it.")
#打印and finally,we close it.
target.close()
#关闭文件
#如果想要查看你自己编写的txt文件,要重新打开open,再read(记住一定是要先进行open!!)
test=open("test.txt")
print(test.read())
#对test执行read命令并且打印结果(这里并没有输入路径名,因为我也不知道刚才写的txt是存在哪里,demo里也没有。没想到这样成功了...)
#——————————————————————分隔符————————————————————————加分训练(自己写一个文件)
file=open('E:\python\demo\ex16+.txt','r+') #新建一个ex16+.txt
file.truncate()
line1=input(">>>>line1:") #更明显的用户提示
line2=input(">>>>line2:")
line3=input(">>>>line3:")
file.write("%s\n%s\n%s\n" %(line1, line2, line3))
#一个优化
#问题?file.write(line1+'\n'+line2+'\n'+line3+'\n')行不通唉
file=open('E:\python\demo\ex16+.txt')
print(file.read())
#——————————————————————-第二种方法————————————————————
file=open('E:\python\demo\ex16+.txt','r+')
file.truncate()
line1=input(">>>>line1:")
line2=input(">>>>line2:")
line3=input(">>>>line3:")
file.write(line1)
file.write(line2)
file.write(line3)
nl='\n'
file.write(nl+line1+nl+line2+nl+line3+nl)
#这也是一种优化的办法,但是先输出所有的lines(在同一行),然后分行输出line1/2/3
file=open('E:\python\demo\ex16+.txt')
print(file.read())

注意:最后做了一个优化,文件中重复的地方太多了。试着用一个target.write() 将line1, line2, line3 打印出来,使用字符串、格式化字符、以及转义字符。

推荐第一个方法,简单直接

5.18-笨办法学python-习题16(write)的更多相关文章

  1. 笨办法学Python - 习题1: A Good First Program

    在windows上安装完Python环境后,开始按照<笨办法学Python>书上介绍的章节进行练习. 习题 1: 第一个程序 第一天主要是介绍了Python中输出函数print的使用方法, ...

  2. 笨办法学Python - 习题11-12: Asking Questions & Prompting People

    目录 1.习题 11: 提问 2.习题 12: 提示别人 3.总结 1.习题 11: 提问 学习目标:了解人机交互场景,熟悉raw_input 的用法. 1.在 Python2.x 中 raw_inp ...

  3. 笨办法学Python - 习题8-10: Printing & Printing, Printing

    目录 1.习题 8: 打印,打印 2.习题 9: 打印,打印,打印 3.习题 10: 那是什么? 3.1.转义序列: 4.习题总结: 1.习题 8: 打印,打印 学习目标:继续学习 %r 的格式化输出 ...

  4. 笨办法学Python - 习题6-7: Strings and Text & More Printing

    目录 1.习题 6: 字符串(string) 和文本 2.加分习题: 3.我的答案 4.习题总结 5.习题 7: 更多打印 6.习题总结 1.习题 6: 字符串(string) 和文本 学习目标:了解 ...

  5. 笨办法学Python - 习题5: More Variables and Printing

    1.习题 5: 更多的变量和打印 学习目标:了解用户输入方法,明白pthon2和Python3之间的用户输入的区别.了解格式化字符串(format string)的概念,学会如何创建包含变量内容的字符 ...

  6. 笨办法学Python - 习题4: Variables and Names

    1.习题 4: 变量(variable)和命名 学习目标:了解Python中变量的定义,学习定义简明易记的变量名 变量:变量是存储内存中的值,就是每定义一个变量就会在内存中开辟一个空间.基于变量的类型 ...

  7. 笨办法学Python - 习题3: Numbers and Math

    目录 习题 3: 数字和数学计算 算术运算符 加分习题: 我的答案: 总结: 扩展: Python比较运算符 Python赋值运算符 Python位运算符 Python逻辑运算符 Python成员运算 ...

  8. 笨办法学python 习题14 优化过 遇到问题的请看

    print "\t what's you name?"user_name = raw_input('>') from sys import argvscript, = arg ...

  9. 笨办法学 Python (Learn Python The Hard Way)

    最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...

  10. 笨办法学 Python (第三版)(转载)

    笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html   摘自https://learn-python ...

随机推荐

  1. layer 遮罩层等待

    效果 代码: js函数之前:var msg = layer.msg('努力中加载中...', {icon: 16,shade: [0.5, '#f5f5f5'],scrollbar: false,of ...

  2. python 反转字符串 [::-1]的问题

    >>> a = '12345' >>> a[:-1]'1234' -1表示最后一个,所以取第一个到最后且不包含最后一个 >>> a[1:4:2]' ...

  3. January 12 2017 Week 2 Thursday

    Although it rains, throw not away your watering pot. 纵然天下雨,休把水壶丢. Don't throw away your watering pot ...

  4. ECharts.js学习(三)交互组件

    ECharts.js 交互组件 ECharts.js有很多的交互组件,一般经常用到的组件有这些: title:标题组件,包含主标题和副标题. legend:图例组件,展现了不同系列的标记(symbol ...

  5. 2、Node.js 第一个应用

    内容:三种变量申明方式,Node.js应用组成,第一个应用创建+代码 ################################################################# ...

  6. JVM线程状态,park, wait, sleep, interrupt, yeild 对比

    ---恢复内容开始--- JVM线程状态 NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED BLOCKED是等待获得对象锁 WAIT ...

  7. 远程登录与文件传输指令——ssh与scp

    远程登录指令 ssh ssh 是一个用于登录远程主机并在远程主机上执行命令的程序.ssh 设计的本意在于在一个不确定的网络环境下为两个互不信任的主机提供加密通信功能.在 Linux 桌面版本上,一般内 ...

  8. Mint-ui 中 Popup 作为组件引入,控制弹出框的显示与隐藏遇到的问题。

    Popup组件的结构: <template>   <div>   <!--分享弹出窗 begin-->     <mt-popup class="s ...

  9. Mac上安装MongoDB

    1.访问MongoDB官方下载地址 http://www.mongodb.org/downloads 2.点击“DOWNLOAD(tgz)”按钮: 3.将下载的文件压缩包解压后剪切到你的Mac中某个位 ...

  10. Modal实现页面跳转和控制器数据传递

    一.Model跳转的实现 1.新建工程 2.新建View控制器和导航控制器 (1)为拖控件,两个view一个navigation; 如图: (2)view的“GotoTwo”按键添加Segues到Na ...