本来面目

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. liunx驱动----点亮LED

    自动挂接根文件系统(直接从NFS启动) 修改uboot命令行 把 bootargs=noinitrd root=/dev/mtdblock3 init=/linuxrc console=ttySAC0 ...

  2. 通过浏览器F12开发工具快速获取别的网站前端代码的方法

    通过浏览器F12开发工具快速获取别的网站前端代码的方法 说明:直接另存为网页是比较老的做法,会有很多没用的东西下载下来.通过F12开发工具,sources获取到的是比较好的,有目录结构的源文件.

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

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

  4. 如何在Ubuntu 16.04上安装PythonGDAL 2.1?

    sudo add-apt-repository -y ppa:ubuntugis/ubuntugis-unstable sudo apt update sudo apt upgrade # if yo ...

  5. flask、tornado、BaseHTTPServer性能简单对比

    最近写了一个web应用,分别用flask.tornado.BaseHTTPServer都实现了一次,顺便就对比了一下三者的性能,本结果仅对本次测试负责(这句话很内涵,值得推广). 测试工具用了ab,时 ...

  6. D9 图论综合题

    1.白银莲花池 LUOGU 2411 第一种思路:当然我们可以写三个bfs a掉这个题,这写下来一二百行要有了吧: 第二种:我们可以在一个bfs中维护所有的信息,一个方向数组,从起点开始,向八个方向扩 ...

  7. macbook突然没有声音了

    我百度了一下 说是苹果系统的一个bug,这是因为 Mac OS X 的核心音频守护进程「coreaudiod」出了问题,虽然简单的重启电脑就能解决,但是如果此时开启了很多程序后者有其他情况不想重启电脑 ...

  8. TCP/IP学习

    1.TCP/IP网络包括两部分 ①传输协议 ②网络协议

  9. SQL Server監控与診斷

    僅為記錄工作中遇到的問題. 1. 字符串截斷: SQL server里很多job用於運行DTS,經常會收到系統出錯警報,如: ...String ) The statement has been te ...

  10. 将pip源设置为国内源的方法

    需要将pip源设置为国内源,阿里源.豆瓣源.网易源等 - windows (1)打开文件资源管理器(文件夹地址栏中) (2)地址栏上面输入 %appdata% (3)在这里面新建一个文件夹 pip ( ...