本来面目

  1. from sys import argv
  2. from os.path import exists
  3. script, from_file, to_file = argv
  4. print(f"Copying from {from_file} to {to_file}")
  5. #we could do these two on noe line, how?
  6. in_file =open(from_file)
  7. indata = in_file.read()
  8. print(f"The input file is {len(indata)} bytes long")
  9. print("Ready, hit the RETURN to countinue, CTRL_c to abort")
  10. input()
  11. out_file = open(to_file,'w')
  12. out_file.write(indata)
  13. print("Alright, all done.")
  14. out_file.close()
  15. in_file.close()

第一次简化

in_file =open(from_file)

indata = in_file.read()

合为 indata = open(from_file).read()

  1. from sys import argv
  2. from os.path import exists
  3. script, from_file, to_file = argv
  4. print(f"Copying from {from_file} to {to_file}")
  5. #we could do these two on noe line, how?
  6. indata = open(from_file).read()
  7. print(f"The input file is {len(indata)} bytes long")
  8. print("Ready, hit the RETURN to countinue, CTRL_c to abort")
  9. input()
  10. out_file = open(to_file,'w')
  11. out_file.write(indata)
  12. #open(to_file,'w').write(open(from_file).read())
  13. print("Alright, all done.")
  14. out_file.close()
  15. in_file.close()

会报错,因为写了 indata = open(from_file).read() 就无序写关闭语句in_file.close()

以为其中的read一旦运行,文件就会被Python关闭掉

  1. from sys import argv
  2. from os.path import exists
  3. script, from_file, to_file = argv
  4. print(f"Copying from {from_file} to {to_file}")
  5. #we could do these two on noe line, how?
  6. indata = open(from_file).read()
  7. print(f"The input file is {len(indata)} bytes long")
  8. print("Ready, hit the RETURN to countinue, CTRL_c to abort")
  9. input()
  10. out_file = open(to_file,'w')
  11. out_file.write(indata)
  12. #open(to_file,'w').write(open(from_file).read())
  13. print("Alright, all done.")
  14. out_file.close()

改成一句话


  1. from sys import argv
  2. from os.path import exists
  3. script, from_file, to_file = argv
  4. open(to_file,'w').write(open(from_file).read())

笨办法学python 文本复制的更多相关文章

  1. 笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘

    笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘 提取码:xaln  怎样阅读本书 由于本书结构独特,你必须在学习时遵守几条规则 录入所有代码,禁止复制粘贴 一字不差地录入代码 ...

  2. 笨办法学Python 3|百度网盘免费下载|新手基础入门书籍

    点击下方即可百度网盘免费提取 百度网盘免费下载:笨办法学Python 3 提取码:to27 内容简介: 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用. ...

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

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

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

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

  5. 《笨办法学 Python(第四版)》高清PDF|百度网盘免费下载|Python编程

    <笨办法学 Python(第四版)>高清PDF|百度网盘免费下载|Python编程 提取码:jcl8 笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机 ...

  6. 笨办法学python 第四版 中文pdf高清版|网盘下载内附提取码

    笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机了解不多,没有学过编程,但对编程感兴趣的朋友学习使用.这本书以习题的方式引导读者一步一步学习编 程,从简单的打印一 ...

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

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

  8. 笨办法学python 13题:pycharm 运行

    笨办法学python 13题 代码: # -*- coding: utf-8 -*- from sys import argv # argv--argument variable 参数变量 scrip ...

  9. 《笨办法学Python 3》python入门书籍推荐|附下载方式

    <笨办法学Python 3>python入门书籍免费下载 内容简介 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用.这本书以习题的方式引导读 ...

随机推荐

  1. Vue 中如何引入第三方 JS 库

    一绝对路径直接引入全局可用 二绝对路径直接引入配置后import 引入后再使用 三webpack中配置 aliasimport 引入后再使用 四webpack 中配置 plugins无需 import ...

  2. 牛客OI周赛7-提高组 A 小睿睿的等式

    链接:https://ac.nowcoder.com/acm/contest/371/A来源:牛客网 小睿睿在游戏开始时有n根火柴棒,他想知道能摆成形如“A+B=n”的等式且使用的火柴棒数也恰好等于n ...

  3. 网页静态化技术--Freemarker入门

    网页静态化技术:为什么要使用网页静态化技术 网页静态化解决方案在实际开发中运用比较多,例如新闻网站,门户网站中的新闻频道或者是文章类的频道. 对于电商网站的商品详细页来说,至少几百万个商品,每个商品又 ...

  4. 妙用valueForKeyPath

    valueForKey与valueForKeyPath在KVC中同时出现,都可以使用,难免让开发者迷惑:心里知道肯定是不一样,但具体的用法你会吗?其实valueForKeyPath的功能更强大,支持深 ...

  5. CentOS 7 yum install cobbler2.8.3

    安装前注意事项: 1.cobbler主机要为静态ip,否则和dhcpd服务冲突. 2.如果用虚拟机安装,client的内存请设置为2g以上,否则会报错. 3.kickstart文件中不要出现中文,大坑 ...

  6. Hibernate的Cascade——级联操作

    在Hibernate中,针对持久化实体的配置文件中有Cascade这样一个属性,顾名思义就是级联,也就是说在操作当 前实体时,针对当前实体的操作会影响到相应配置的关联实体.比如针对当前实体进行保存操作 ...

  7. vml--基础

    VML VML是The Vector Markup Language(矢量可标记语言)的缩写.官网:https://www.vml.com/ ex: <!DOCTYPE html> < ...

  8. 0x13链表与邻接表之邻值查找

    题目链接:https://www.acwing.com/problem/content/138/ 参考链接:https://blog.csdn.net/sdz20172133/article/deta ...

  9. The History of spring

    Spring的出现  Spring最早出现对早期J2EE规范复杂性的回应 .虽然有些人一直认为Java EE和Spring处于竞争中,但Spring实际上是对Java EE的补充.Spring编程模型 ...

  10. Lintcode177-Convert Sorted Array to Binary Search Tree With Minimal Height-Easy

    177. Convert Sorted Array to Binary Search Tree With Minimal Height Given a sorted (increasing order ...