【Python】 2种实现文件复制的方法
来源:互联网 发布:redis mac 客户端 编辑:程序博客网 时间:2024/06/09 21:52
#coding:utf-8# 方法1:使用read()和write()模拟实现文件拷贝# 创建文件hello.txtsrc = file("hello.txt", "w")li = ["Hello world \n", "Hello China \n"]src.writelines(li)src.close()#把hello.txt 拷贝到hello2.txtsrc = file("hello.txt", "r")dst = file("hello2.txt", "w")dst.write(src.read())src.close()dst.close()# 方法2:使用shutil模块# shutil模块是一个文件、目录的管理接口,提供了一些用于复制文件、目录的函数# copyfile()函数可以实现文件的拷贝# copyfile(src, dst)# move()函数实现文件的剪切# move(src, dst)import shutilshutil.copyfile("hello.py", "hello2.py") #hello.txt内容复制给hello2.txtshutil.move("hello.py", "../") #hello.txt复制到当前目录的父目录,然后删除hello.txtshutil.move("hell2.txt", "hello3.txt") #hello2.txt移到当前目录并命名为hello3.py, 然后删除hello2.txt
0 0
- 【Python】 2种实现文件复制的方法
- Python 复制文件的方法
- java 实现文件复制,二种方法.
- Python 实现文件复制、删除
- Python 实现文件复制、删除
- Python 实现文件复制、删除
- Python 实现文件复制、删除
- python 实现文件复制,删除
- 缓冲区方法实现复制文件
- Java 实现文件复制方法
- [Python]列表复制的几种方法
- 实现文件的复制
- 实现文件的复制
- python实现文件和目录的复制和删除
- python实现将某类文件复制到特定的目录下
- python基础实现桌面上txt文件的复制
- Java 文件复制的4种方法
- Java 文件复制的4种方法
- valgrind 检查内存泄漏
- super与this的用法
- 第七周 项目3-负数把正数赶出队列
- Java运行时内存区域
- git commit 中的change-id
- 【Python】 2种实现文件复制的方法
- 一个理性战胜感性的成功案例:P2P投资和活期理财,纠结中提炼出来的1个数学问题
- git 远程回滚
- NCPC 2015 A Adjoin the Networks(好题)
- 近期感悟
- linux_module初学回顾
- NGUI UIinput 取消移动端拖动输入框弹出输入键盘
- 关于libpointmatcher
- windbg符号下载