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. npm 使用国内镜像的方法

    npm全称Node Package Manager,是node.js的模块依赖管理工具.由于npm的源在国外,所以国内用户使用起来各种不方便.我们通过设置使用淘宝的镜像来加快我们的速度. 临时使用 n ...

  2. 使用python 操作liunx的svn,方案二

    在对liunx操作svn的方式,做了改动,使用python的,subprocess进行操作 在第一种方案中,我使用了先拉到本地,然后再创建,在进行上传,实际在svn中可以直接创建文件,并进行文件复制, ...

  3. 【1】【MOOC】Python游戏开发入门-北京理工大学【第二部分-游戏开发之框架】

    学习地址链接:http://www.icourse163.org/course/0809BIT021E-1001873001?utm_campaign=share&utm_medium=and ...

  4. virtualbox+vagrant学习-2(command cli)-1-vagrant box命令

    vagrant box 这是用于管理(添加.删除等)boxes的命令. box 是一个打包好的操作系统,是一个后缀名为 .box 的文件,其实是一个压缩包,里面包含了 Vagrant 的配置信息和 V ...

  5. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  6. Android App的签名打包(晋级篇)

    http://blog.csdn.net/linghu_java/article/details/6701666 Andriod应用程序如果要在手机或模拟器上安装,必须要有签名! 1.签名的意义 为了 ...

  7. 算法学习记录-查找——平衡二叉树(AVL)

    排序二叉树对于我们寻找无序序列中的元素的效率有了大大的提高.查找的最差情况是树的高度.这里就有问题了,将无序数列转化为 二叉排序树的时候,树的结构是非常依赖无序序列的顺序,这样会出现极端的情况. [如 ...

  8. 一点一点看JDK源码(〇)

    一点一点看JDK源码(〇) liuyuhang原创,未经允许进制转载 写在前面: 几乎所有的大神都会强调看源码,也强调源码的重要性: 但是如何看源码,源码看什么?看了什么用?看了怎么用? 困扰很多人, ...

  9. Maven 高级应用

    Maven 的高级应用主要体现在 ==依赖==,==聚合==,==继承== * 依赖 就是在当前项目的pom.xml 总引入依赖的坐标 最最经常用到的 <dependencies> < ...

  10. Java并发编程(六)原子性与易变性

    原子性 原子是最小单元.不可再分的意思.原子性是指某个操作在获取CPU时间时,要么就给它足够时间,让这个操作执行完,要么就不执行这个操作,执行时不能出现上下文切换(把CPU时间从一个线程分配到另一个线 ...