本来面目

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. python小知识点随笔

    可能会有不局限于Python语言的内容,文中“>>>”表示结果,而不是命令行交互的提示符 1.关于字符串编码 ASCII编码 占用一个字节 二进制11111111=十进制255 所以 ...

  2. RESTClient 使用

    Wisdom RESTClient 一款自动化测试REST API的工具,它可以自动化测试RESTful API并生成精美的测试报告,同时基于测试过的历史API,可以生成精美的RESTful API文 ...

  3. CentOS7操作Redis4.0

    单机安装 1. 从官网下载 redis-4.0.10.tar.gz 到本地,然后上传到VMware虚拟机上,存放地址随意. 2. 解压: tar -zxvf redis-4.0.10.tar.gz 3 ...

  4. Yii1操作phpexcel

    Yii::import('application.vendors.phpexcel.*'); Yii::import('application.vendors.phpexcel.PHPExcel.*' ...

  5. Guitar Por如何演奏刮弦

    每当我们听到吉他现场演出的时候,看到吉他手在激烈的刮弦时,都觉得很酷,非常有感染力.刮弦在我们弹吉他或编曲时,会经常用到,虽然时间很短,但会为你加分不少. 那么我们应该如何演奏刮弦呢,我们先用E5和弦 ...

  6. tcp的三次握手,四次挥手

    为了更好的记住知识点,所以将最近学习的知识点记录下来: 最开始A和B都处于closed(关闭连接状态) 1.tcp的第一次握手:客户端A  向服务器端B 发送请求连接报文段(包含SYN=1,初始序号s ...

  7. python进阶(一)

    一.调试Debugging应用 (1)命令行内运行 $ python -m pdb my_script.py 这会触发debugger在脚本第⼀⾏指令处停⽌执⾏.这在脚本很短时会很有帮助.你可以通过( ...

  8. 【BUAA-OO】第一单元作业总结

    #OO第一单元作业总结 #确认存活,爱学习,爱北航,爱OO 一.三次作业分析 1.第一次作业 1.1 程序结构 对方法的度量: 类的内聚和相互间的耦合情况: 类图: 优缺点: 优点大概没什么优点,毕竟 ...

  9. ATS 相关

    启动命令 /usr/local/CCTS/bin/trafficserver   start |stop | restart 清盘 /usr/local/CCTS/bin/traffic_server ...

  10. 支持向量机(Support Vector Machine):对偶

    前言 学SVM看到对偶问题的时候很难受,因为看不懂,数学知识真的太重要了.后来在B站看到某up主的精彩推导,故总结如下. SVM基本型 由之前最大化间隔的计算可得SVM的基本型为: $\underse ...