Python如何复制文件中的内容
python复制文件中内容的方法:
1、使用shutil.copyfile(file1,file2)方法复制
file1为需要复制的源文件的文件路径,file2为目标文件的文件路径+文件名.
如下:将c盘中A文件夹下的0.png复制到D盘中B文件夹下并重命名为1.png.
src_file='C:\\A\\0.png'
dst_file='D:\\B\\1.png'
shutil.copyfile(src_file,dst_file)
2、使用.shutil.copy(文件1,文件2)方法复制
defcopy(src,dst):
"""copydataandmodebits("cpsrcdst")
Thedestinationmaybeadirectory.
"""
ifos.path.isdir(dst):
dst=os.path.join(dst,os.path.basename(src))
copyfile(src,dst)
copymode(src,dst)
3、shutil.copyfileobj(文件1,文件2):将文件1的数据覆盖copy给文件2。
importshutil
f1=open("1.txt",encoding="utf-8")
f2=open("2.txt","w",encoding="utf-8")
shutil.copyfileobj(f1,f2)
以上内容为大家介绍了Python如何复制文件中的内容,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python cmd中怎么运行python文件
运行Python,可在交互模式下运行,或者命令行中。命令行中运行的是Python的.py文件。cmd中执行Python程序的方法,如下打开cmdC:\Users\Administ...详情>>
2023-11-06 14:37:04
pythondict是啥意思
Python中的dict表示的字典数据类型。字典是另一种可变容器模型,且可存储任意类型对象。字典是python语言中唯一的映射类型映射类型对象里哈希值...详情>>
2023-11-06 13:56:05
python如何创建模块
Python模块(Module),是一个Python文件,以.py结尾,包含了Python对象定义和Python语句。模块让你能够有逻辑地组织你的Python代码段。把相关的...详情>>
2023-11-06 13:36:31
python解释器写代码如何换行
使用以下方法可以使Python的代码换行:用反斜杠链接多行代码,示例输出:55a=22+\33printa用小括号括起来,示例输出:55a=(22+33)printa提示:针...详情>>
2023-11-06 13:17:12热门推荐
技术干货






