练习17——更多文件操作
2016-07-01 14:35
274 查看
# -*- coding: utf-8 -*- #更多文件操作——从A读取数据写入到B #从自带库中导入argv和exists函数 from sys import argv from os.path import exists script, from_file, to_file = argv print ("Coping from %s to %s" % (from_file, to_file)) #打开文件from_file my_input = open(from_file) #给indata赋值为输入文件的数据 indata = my_input.read() #len函数用来判断数据的大小 print ("The input file is %d bytes long." % len(indata)) #exists函数用来判断文件是否存在 print ("Does the output file exists? %r\nReady ,hit Return to continue, Ctrl-C to abort" % exists(to_file)) #这边用输入作为程序的停顿,以后可能会有更好的办法 input() my_output = open(to_file, 'w') my_output.write(indata) print ("Alright, all done.") #最后不忘关文件。 my_input.close() my_output.close() #进阶问题1:这个脚本 实在是 有点烦人。没必要在拷贝之前问一遍把,没必要在屏幕上输出那么多东西。 #试着删掉脚本的一些功能,让它使用起来更加友好。 from sys import argv script, from_file, to_file = argv print ("Coping from %s to %s" % (from_file, to_file)) my_input = open(from_file) indata = my_input.read() my_output = open(to_file, 'w') my_output.write(indata) print ("Alright, all done.") my_input.close() my_output.close() #进阶问题2:看看你能把这个脚本改多短,我可以把它写成一行。 #如下一行,可以实现功能。(不关闭文件可能会有问题,以后再想这个问题。) open("C:/PE/FC2.txt", 'w').write(open("C:/PE/FC.txt").read())
相关文章推荐
- HTTP请求方法对照表和请求状态
- UDP穿洞技术,P2P一些原理
- linux下安装tomcat的默认目录
- 由一段shell脚本想到的知识
- 为Linux上的Tomcat安装apr支持
- Oracle JDK/JRE下载
- 使用 HTML5 input 类型提升移动端输入体验
- unbind() 移除事件内处理方法
- DTrace to Troubleshoot Java Native Memory Problems
- java面向对象面试题(3)
- 史上最强大的Saiku开发资源总结
- playbook
- C# 执行cmd命令,以进程的形式执行应用程序,在新的线程中执行耗时的功能逻辑
- android studio 拿到sha1
- $.ajax()方法参数详解
- 查询会龄 sql部分
- Spring <bean> 参数意义
- 验证 结构体指针与自增运算符
- 业务零影响!如何在Online环境中巧用MySQL传统复制技术【转】
- Android跳转到微信公众号