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. Linux 系统的文件类型

    Linux文件类型常见:普通文件.目录文件.字符设备文件和块设备文件.符号链接文件 1.1. 普通文件(-) ls -lh 来查看某个文件的属性,可以看到有类似-rwxrwxrwx,值得注意的是第一个 ...

  2. 毫秒级百万数据分页存储过程(mssql)

    /****** Object: StoredProcedure [dbo].[up_Page2005] Script Date: 11/28/2013 17:10:47 ******/ SET ANS ...

  3. ZT 打工者买彩票中1000万 5年后变逃犯身上剩80元

    打工者买彩票中1000万 5年后变逃犯身上剩80元   2014-01-07 08:22 来源:都市快报 我有话说 挥霍—— 从800万到80元 在湖南永州零陵区富家桥镇茶叶湾村,陈某是不折不扣的名人 ...

  4. [转载] c++对结构体数组排序

    按结构体中某个成员进行排序,下面代码以成员b1为升序的排序 代码: #include<iostream> #include<algorithm> using namespace ...

  5. idea 2018激活注册码

    我使用的方法和pycharm激活类似,激活码不同而已pycharm激活步骤链接:可以新建标签打开https://www.cnblogs.com/-nbloser/p/8570648.html idea ...

  6. less使用总结

    15年自学了 less ,可是一直没用,就忘记了.后来抱着提高 css 开发速度的目的,又去学习了 less ,学完马上用,效果立竿见影,记得也牢了.刚开始学习前,我们总会问自己一个问题,学习它有什么 ...

  7. 【node.js】事件循环、EventEmitter

    Node.js 是单进程单线程应用程序,但是通过事件和回调支持并发,所以性能非常高. Node.js 的每一个 API 都是异步的,并作为一个独立线程运行,使用异步函数调用,并处理并发. 事件驱动程序 ...

  8. Hadoop 解除 NameNode is in safe mode

    运行Hadoop程序时,有时候会报以下错误: org.apache.hadoop.dfs.SafeModeException: Cannot delete /user/hadoop/input. Na ...

  9. Spring(十三)之SQL存储过程

    SimpleJdbcCall 类可以被用于调用一个包含 IN 和 OUT 参数的存储过程.你可以在处理任何一个 RDBMS 时使用这个方法,就像 Apache Derby, DB2, MySQL, M ...

  10. spring cloud config将配置存储在数据库中

    Spring Cloud Config Server最常见是将配置文件放在本地或者远程Git仓库,放在本地是将将所有的配置文件统一写在Config Server工程目录下,如果需要修改配置,需要重启c ...