【求教】python中如何将指定目录下的文件(文件夹)拷贝到指定目录

thecorr 2009-12-03 03:18:21
如题
...全文
515 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
luotuo512 2009-12-03
  • 打赏
  • 举报
回复
windows xp sp3测试通过。修改source和target_dir即可。相见http://blog.csdn.net/luotuo512/archive/2009/09/03/4517229.aspx和python帮助文档。
def moveoldfile():
k=0
source = 'e:\\xdj\\voa\\new'
target_dir = 'e:\\xdj\\voa'
filelist=listdir(source)
print 'move old files:\n',listdir(source)
for name in filelist :
srcFilename = source + '\\' + name
srcFilename = '"' + srcFilename + '"'
# desFilename = target_dir + '\\' + now + '_' + name
desFilename = '"' + target_dir + '"'
# print
copy_command = "move /Y %s %s" % (srcFilename, desFilename)
# print copy_command
if os.system(copy_command) == 0:
k = k + 1
# print 'Successful backup to copy from', srcFilename, 'to' ,desFilename
else:
print 'Fail to copy', srcFilename, 'to', desFilename
print 'total move', k, 'files'
notax 2009-12-03
  • 打赏
  • 举报
回复
http://docs.python.org/library/shutil.html#shutil.copytree


shutil.copytree(src, dst[, symlinks=False[, ignore=None]])

Recursively copy an entire directory tree rooted at src. The destination directory, named by dst, must not already exist; it will be created as well as missing parent directories. Permissions and times of directories are copied with copystat(), individual files are copied using copy2().

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧