笨办法学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?
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 文本复制的更多相关文章
- 笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘
笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘 提取码:xaln 怎样阅读本书 由于本书结构独特,你必须在学习时遵守几条规则 录入所有代码,禁止复制粘贴 一字不差地录入代码 ...
- 笨办法学Python 3|百度网盘免费下载|新手基础入门书籍
点击下方即可百度网盘免费提取 百度网盘免费下载:笨办法学Python 3 提取码:to27 内容简介: 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用. ...
- 笨办法学 Python (Learn Python The Hard Way)
最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...
- 笨办法学 Python (第三版)(转载)
笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html 摘自https://learn-python ...
- 《笨办法学 Python(第四版)》高清PDF|百度网盘免费下载|Python编程
<笨办法学 Python(第四版)>高清PDF|百度网盘免费下载|Python编程 提取码:jcl8 笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机 ...
- 笨办法学python 第四版 中文pdf高清版|网盘下载内附提取码
笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机了解不多,没有学过编程,但对编程感兴趣的朋友学习使用.这本书以习题的方式引导读者一步一步学习编 程,从简单的打印一 ...
- 笨办法学Python - 习题1: A Good First Program
在windows上安装完Python环境后,开始按照<笨办法学Python>书上介绍的章节进行练习. 习题 1: 第一个程序 第一天主要是介绍了Python中输出函数print的使用方法, ...
- 笨办法学python 13题:pycharm 运行
笨办法学python 13题 代码: # -*- coding: utf-8 -*- from sys import argv # argv--argument variable 参数变量 scrip ...
- 《笨办法学Python 3》python入门书籍推荐|附下载方式
<笨办法学Python 3>python入门书籍免费下载 内容简介 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用.这本书以习题的方式引导读 ...
随机推荐
- 结合API Gateway和Lambda实现登录时的重定向和表单提交请求(Python3实现)
1. 创建Lambda函数,代码如下: from urllib import parse def lambda_handler(event, context): body = event['body' ...
- 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165215
2018-2019-2 <网络对抗技术>Exp0 Kali安装 Week1 20165215 目录 Kali的下载及安装 网络配置 设置共享文件夹及剪切板 更新软件源 [Kali的下载及安 ...
- Linux服务器---博客wordpress
Wordpress Wordpress是一个开源的博客平台,是搭建个人博客的首选,用户可以去wordpress中文网站寻找帮助资料 1.下载wordpress软件(https://cn.wordpre ...
- PowerDesigner 15的Table表视图的列显示Code
PowerDesigner 15的图表的Table表视图一般显示成这样: 现在,我要将Code显示到Table表视图上,该怎么做?选择菜单:Tools→Display Preferences,弹出对话 ...
- .NET Core 微服务
github 上的资源: EshopOnContainers AiurSoft : 微服务框架 Server酱:是一款「程序员」和「服务器」之间的通信软件. http://sc.ftqq.com/3. ...
- php 随机红包算法
<?php /** * 红包分配算法 * * example * $coupon = new Coupon(200, 5); * $res = $coupon->handle(); * p ...
- Vue 组件&组件之间的通信 之 子组件向父组件传值
子组件向父组件传值:子组件通过$.emit()方法以事件形式向父组件发送消息传值: 使用步骤: 定义组件:现有自定义组件com-a.com-b,com-a是com-b的父组件: 准备获取数据:父组件c ...
- NABCD原则
1.我们的产品 <随堂小测APP> 是为了解决 <老师们> 的痛苦, 2-N.他们需要 随时组织课堂测验, 但是现有的方案并没有很好地解决这些需求,3-A.我们有独特的办法 ...
- 使用pynlpir增强jieba分词的准确度
在使用jieba分词时,发现分词准确度不高.特别是一些专业词汇,比如堡垒机,只能分出堡垒,并不能分出堡垒机.这样导致的问题是很多时候检索并不准确. 经过对比测试,发现nlpir进行分词效果更好.但是n ...
- iOS SDWebImage知识点
1.clear 和 clean clear 先把之前的缓存文件夹删除掉,然后新建一个文件夹 clean 先删除过期的文件,然后计算剩余缓存文件的大小 currentSize > maxSize, ...