记录几个比较常用的python复制文件的方法

python复制文件的方式有很多个,针对不同的目的有不同的操作,可以对文件权限,描述信息进行复制或者不复制操作,这里只记录了几个我日常会用到的

from shutil import copyfile,copy,copyfileobj
import os
help(copy)
# Help on function copy in module shutil:

# copy(src, dst, *, follow_symlinks=True)
#     Copy data and mode bits ("cp src dst"). Return the file's destination.
    
#     The destination may be a directory.
    
#     If follow_symlinks is false, symlinks won't be followed. This
#     resembles GNU's "cp -P src dst".
    
#     If source and destination are the same file, a SameFileError will be
#     raised.
#该函数会同时拷贝数据以及数据的权限,如果目标为目录时将会以原文件名保存;如果已经存在和目标文件名相同的文件,则会报错

help(copyfile)
# Help on function copyfile in module shutil:

# copyfile(src, dst, *, follow_symlinks=True)
#     Copy data from src to dst.
    
#     If follow_symlinks is not set and src is a symbolic link, a new
#     symlink will be created instead of copying the file it points to.
#该函数智慧复制文件内容 而不会去复制权限及其他文件的描述信息,所以会比copy快一点
help(os.popen)
# Help on function popen in module os:

# popen(cmd, mode='r', buffering=-1)
#     # Supply os.popen()
#该命令可以调用系统命令去进行文件的复制操作,不过在复制时创建一个指向或来自该命令的管道。它返回一个连接到管道的打开的文件对象。您可以根据文件打开模式(即’r’(默认)或’w’)使用它进行读取或写入
#模式 –它可以是’r’(默认)或’w’。
#bufsize –如果其值为0,则不会发生缓冲。如果设置为1,则在访问文件时将进行行缓冲。如果您提供的值大于1,则缓冲将以指定的缓冲区大小进行。但是,对于负值,系统将采用默认缓冲区大小
os.popen('copy x x1')#windows
os.popen('cp x x2')#linux
help(os.system)
# Help on built-in function system in module nt:

# system(command)
#     Execute the command in a subshell.
os.popen('copy x x1')#windows
os.popen('cp x x2')#linux

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值