本来面目

from sys import argv
from os.path import exists script, from_file, to_file = argv print(f"Copying from {from_file} to {to_file}") #we could do these two on noe line, how?
in_file =open(from_file)
indata = in_file.read() print(f"The input file is {len(indata)} bytes long")
print("Ready, hit the RETURN to countinue, CTRL_c to abort")
input() out_file = open(to_file,'w')
out_file.write(indata) print("Alright, all done.") out_file.close()
in_file.close()

第一次简化

in_file =open(from_file)

indata = in_file.read()

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

from sys import argv
from os.path import exists script, from_file, to_file = argv print(f"Copying from {from_file} to {to_file}") #we could do these two on noe line, how?
indata = open(from_file).read() print(f"The input file is {len(indata)} bytes long")
print("Ready, hit the RETURN to countinue, CTRL_c to abort")
input() out_file = open(to_file,'w')
out_file.write(indata)
#open(to_file,'w').write(open(from_file).read())
print("Alright, all done.") out_file.close()
in_file.close()

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

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

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

改成一句话


from sys import argv
from os.path import exists script, from_file, to_file = argv 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. rpm包与 yum 安装与卸载

    rpm包的安装:      1.安装一个包 # rpm -ivh 2.升级一个包 # rpm -Uvh 3.移走一个包 # rpm -e 4.安装参数 --force 即使覆盖属于其它包的文件也强迫安 ...

  2. @Conditional 原理

    1,这里讲的是:org.springframework.context.annotation.Conditional 2,在springConfig文件里注册bean @Conditional(Col ...

  3. springboot整合JPA(简单整理,待续---)

    整合步骤 引入依赖: <dependencies> <dependency> <groupId>org.springframework.boot</group ...

  4. ELK学习笔记之ELK搜集OpenStack节点日志

    模板来自网络,模板请不要直接复制,先放到notepad++内调整好格式,注意缩进 部署架构 控制节点作为日志服务器,存储所有 OpenStack 及其相关日志.Logstash 部署于所有节点,收集本 ...

  5. Web 前端编程运维必备

    Html 1.Html 标签初知 2.Html 标签种类 3.Html 符号 4.Html Title 标签 5.Html meta 标签 6.Html Link 标签 7.Html p 标签 8.H ...

  6. mysql 数据库的设计三范式

    三范式 1NF:字段不可分; 2NF:有主键,非主键字段依赖主键; 3NF:非主键字段不能相互依赖; 解释: 1NF:原子性 字段不可再分,否则就不是关系数据库; 2NF:唯一性 一个表只说明一个事物 ...

  7. opencv检错:程序运行过程正常,当跳出函数时出现断言错误(Debug Assertion Failed)

    转载http://blog.csdn.net/u012327581/article/details/51351780 1.问题描述 在VS2015下配置好Opencv后,程序在函数运行过程中正常,调试 ...

  8. mybatis第一次搭建出错

    ### Error building SqlSession. ### The error may exist in com/test/pojo/UserMapper.xml ### Cause: or ...

  9. 微信小程序开发——简记

    MVVM:提倡渲染(页面)和逻辑分离.页面(DOM),逻辑(后端代码JS等)—> JS操纵DOM.通过模板引擎,操纵和表现. 页面与程序,如何配合工作: 微信客户端会根据page.json配置, ...

  10. IPTABLES使用总结(内网模拟银行网络)

    iptables中有以下三种类型的表: FILTER表,默认的表,包含以下三种内建链: INPUT链,发给本地sockets的包 FORWARD链,经由系统发送的包 OUTPUT链,本地生成并发出的包 ...