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. 月报 提取/保存 到OneDrive. 并发送反馈邮件

  2. August 01st 2017 Week 31st Tuesday

    A contented mind is the greatest blessing a man can enjoy in this world. 知足是人生在世最大的幸事. Being content ...

  3. 如何恢复在Windows 10中被永久删除的照片?

    照片被误删除了需要恢复?这里推荐一款软件:winutilities.使用WinUtilities文件恢复向导允许您通过简单的点击恢复已删除的照片或从Windows 10回收站中恢复被删除的照片. 恢复 ...

  4. ubuntu 13.04添加 flash_plugin

    mv libflashplayer.so  /usr/lib/mozilla/plugins

  5. [零基础学JAVA]Java SE面向对象部分.面向对象基础(05)

    1.继承 2.多态 3.final 4.重载与覆写 5. this/super 6.抽象类 7.接口 java: class Person{ private String name;    priva ...

  6. java简单的工厂模式

    定义:专门定义一个类来创建其他类的实例,被创建的实例通常都具有共同的父类和接口.意图:提供一个类由它负责根据一定的条件创建某一及具体类的实例 //简单工厂,存在不符合开闭原则的地方,可以在参考抽象工厂 ...

  7. Unable to perform unmarshalling at line number 16 and column 63 in RESOURCE hibernate.cfg.xml. Message: cvc-elt.1: 找不到元素 'hibernate-configuration' 的声明。

    七月 02, 2017 4:32:37 下午 org.hibernate.Version logVersionINFO: HHH000412: Hibernate Core {5.2.10.Final ...

  8. 绕过disable_functions执行命令实验

    绕过disable_functions执行命令实验 看下disable函数,所有命令函数都被禁用: 编译64位共享库: 命令成功执行: 参考链接: https://www.freebuf.com/ar ...

  9. 20145314郑凯杰 《Java程序设计》第10周学习总结

    20145314郑凯杰 <Java程序设计>第10周学习总结 代码托管: 学习内容总结 网络编程 会打手机吗? 第一个问题:会打手机吗?很多人可能说肯定会啊,不就是按按电话号码,拨打电话嘛 ...

  10. No.6 - 利用 CSS animation 制作一个炫酷的 Slider

    *{ margin:; padding:; } div{ margin: auto; width: 800px; height: 681px; position: relative; overflow ...