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. MFC连接Mysql数据库执行查询和插入

    配置环境: include:mysql.h文件 lib:libmysql.lib文件 dll::libmysql.dll文件 连接代码: MYSQL m_sqlCon; MYSQL_RES *m_re ...

  2. java获取中文拼音首字母

    import net.sourceforge.pinyin4j.PinyinHelper; public class PinyinHelperUtil { /** * 得到中文首字母(中国 -> ...

  3. 沉淀再出发:Tomcat的实现原理

    沉淀再出发:Tomcat的实现原理 一.前言 在我们接触java之后,相信大家都编写过服务器程序,这个时候就需要用到Tomcat了.Tomcat 服务器是一个开源的轻量级Web应用服务器,在中小型系统 ...

  4. August 19th 2017 Week 33rd Saturday

    Live for today and stop stressing out about tomorrow. 为今天而活,不必为明天过分担心. Stop bewailing the past misfo ...

  5. 防止开放重定向,恶意篡改returnUrl

    1.防止开放重定向: /// <summary> /// 防止开放重定向 /// </summary> /// <param name="url"&g ...

  6. Git commit comment 汇总标准

    参考汇总互联网其它文章建议,结合PEP 257 Docstring Conventions的描述,总结的Git 注释风格,作为个人执行的标准.内容如下: 遵循标准: 1,所有注释尽量坚持使用英文,如果 ...

  7. 有关js弹出提示框几种方法

    1直接提示只有确定功能的提示框 只显示提示信息 alert(“提示信息”); alert ();的参数只有一个就是提示信息,无返回值 2 弹出输入框让你输入内容 prompt() ; 有两个参数:第一 ...

  8. 在 ServiceModel 客户端配置部分中,找不到引用协定“myservice.Service1Soap”的默认终结点元素。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素中找不到与此协定匹配的终结点元素。

    在做项目的时候遇到这个问题,当我在web网站中引用webservice时,很正常,但是当我在一个类库里引用并调用webservice方法后,然后网站调用这个类库里的方法,就会报标题这样的错误.最后纠结 ...

  9. 弹框插件self(动效兼容到IE9,功能兼容IE6)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  10. URAL-1039 Anniversary Party---树形DP入门题

    题目链接: https://cn.vjudge.net/problem/URAL-1039 题目大意: 开一个party,每个员工都有一个欢乐值,只有是上司和下属不同时存在时才能欢乐,问怎样安排能有最 ...