美文网首页
Python-shutil-01-复制文件

Python-shutil-01-复制文件

作者: Data_Python_VBA | 来源:发表于2019-02-25 20:57 被阅读0次

微信公众号原文

系统:Windows 7
语言版本:Anaconda3-4.3.0.1-Windows-x86_64
编辑器:pycharm-community-2016.3.2

  • 这个系列讲讲shutil模块常用功能
  • 本文介绍:复制文件

Part 1:代码

import shutil
import os


current_address = os.path.dirname(os.path.abspath(__file__))
old_file_name = "old.txt"
old_file_address = os.path.join(current_address, old_file_name)

new_file_name = "new.txt"
new_file_address = os.path.join(current_address, new_file_name)

shutil.copyfile(old_file_name, new_file_address)


代码截图

3.png

未执行代码前

1.png

执行代码后

2.png

Part 2:部分代码解读

  1. shutil.copyfile(原文件, 新文件),复制原文件新文件
  2. 新文件已经存在时,会直接覆盖(效果类似删除已经存在的新文件,再复制)

本文为原创作品,欢迎分享朋友圈

常按图片识别二维码,关注本公众号
Python 优雅 帅气


12x0.8.jpg

相关文章

网友评论

      本文标题:Python-shutil-01-复制文件

      本文链接:https://www.haomeiwen.com/subject/jcrnyqtx.html