前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >习题17:更多文件操作

习题17:更多文件操作

作者头像
py3study
发布2018-08-02 14:42:14
2000
发布2018-08-02 14:42:14
举报
文章被收录于专栏:python3python3

脚本很短,不过它会让你对于文件操作有更多的了解

代码如下

代码语言:javascript
复制
# coding: utf-8
__author__ = 'www.py3study.com'
from sys import argv
from os.path import exists

script, from_file, to_file = argv
print("Copying from {} to {}".format(from_file, to_file))
in_file = open(from_file)
indata = in_file.read()
print("The input file is {} bytes long".format(len(indata)))
print("Does the output file exist ? {}".format(exists(to_file)))
print("Ready, hit return to continue, CTRL-C to about.")
input()
out_file = open(to_file, 'w')
out_file.write(indata)
print("Alright, all done.")
out_file.close()
in_file.close()

import了又一个很好用的命令exists,这个命令将文件名字符串作为参数,如果文件存在的话,它将返回True,否则将返回False

通过使用import,你可以在自己代码中直接使用其它更厉害的程序员写的大量免费代码,这样你就不需要重写一遍了

应该看到的结果

图片.png
图片.png

常见问题

为什么'w'要放括号中?

因为这是一个字符串,表示写的意思

len()函数的功能是什么?

它会以数字的形式返回你传递的字符串长度

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-11-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档