700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python复制 移动文件到指定文件夹

python复制 移动文件到指定文件夹

时间:2021-10-03 13:08:11

相关推荐

python复制 移动文件到指定文件夹

之前修改博客 python 复制/移动文件 - clq.lib - 博客园的代码,为保存故作记录。

1、复制文件到指定文件夹

# srcfile 需要复制、移动的文件 # dstpath 目的地址import osimport shutilfrom glob import globdef mycopyfile(srcfile,dstpath): # 复制函数if not os.path.isfile(srcfile):print ("%s not exist!"%(srcfile))else:fpath,fname=os.path.split(srcfile) # 分离文件名和路径if not os.path.exists(dstpath):os.makedirs(dstpath) # 创建路径shutil.copy(srcfile, dstpath + fname)# 复制文件print ("copy %s -> %s"%(srcfile, dstpath + fname))src_dir = './'dst_dir = './copy/'# 目的路径记得加斜杠src_file_list = glob(src_dir + '*')# glob获得路径下所有文件,可根据需要修改for srcfile in src_file_list:mycopyfile(srcfile, dst_dir) # 复制文件

2、移动文件到指定文件夹

# srcfile 需要复制、移动的文件 # dstpath 目的地址import osimport shutilfrom glob import globdef mymovefile(srcfile,dstpath): # 移动函数if not os.path.isfile(srcfile):print ("%s not exist!"%(srcfile))else:fpath,fname=os.path.split(srcfile) # 分离文件名和路径if not os.path.exists(dstpath):os.makedirs(dstpath) # 创建路径shutil.move(srcfile, dstpath + fname)# 移动文件print ("move %s -> %s"%(srcfile, dstpath + fname))src_dir = './'dst_dir = './move/'# 目的路径记得加斜杠src_file_list = glob(src_dir + '*')# glob获得路径下所有文件,可根据需要修改for srcfile in src_file_list:mymovefile(srcfile, dst_dir) # 移动文件

提示:

1、如需移动一个文件,只需将glob中的 ‘*’ 更改为 具体文件名

2、如需移动多个文件,只需根据文件名规则更改glob

3、使用命令行移动文件

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。