Python os.path和shutil模块实现文件复制、删除

     主要运用os.path和shutil模块来在实现文件复制、删除,可以根据自己的需求修改相关代码即可。

 
 
  1. #!/usr/bin/env python 
  2. # -*- coding: utf-8 -*- 
  3.  
  4. import os 
  5. import os.path 
  6. import shutil 
  7. import time,datetime 
  8.  
  9. def copyFiles(sourceDir,  targetDir): #把某一目录下的所有文件复制到指定目录中 
  10.      if sourceDir.find(".svn") > 0
  11.          return 
  12.      for file in os.listdir(sourceDir): 
  13.          sourceFile = os.path.join(sourceDir,  file) 
  14.          targetFile = os.path.join(targetDir,  file) 
  15.          if os.path.isfile(sourceFile): 
  16.              if not os.path.exists(targetDir): 
  17.                  os.makedirs(targetDir) 
  18.              if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))): 
  19.                      open(targetFile, "wb").write(open(sourceFile, "rb").read()) 
  20.          if os.path.isdir(sourceFile): 
  21.              First_Directory = False 
  22.              copyFiles(sourceFile, targetFile) 
  23.  
  24. def removeFileInFirstDir(targetDir):#删除一级目录下的所有文件 
  25.     for file in os.listdir(targetDir): 
  26.         targetFile = os.path.join(targetDir,  file) 
  27.         if os.path.isfile(targetFile): 
  28.             os.remove(targetFile) 
  29.  
  30. def coverFiles(sourceDir,  targetDir):#复制一级目录下的所有文件到指定目录 
  31.          for file in os.listdir(sourceDir): 
  32.              sourceFile = os.path.join(sourceDir,  file) 
  33.              targetFile = os.path.join(targetDir,  file) 
  34.              #cover the files 
  35.              if os.path.isfile(sourceFile): 
  36.                  open(targetFile,"wb").write(open(sourceFile,"rb").read()) 
  37.  
  38. def moveFileto(sourceDir,  targetDir):#复制指定文件到目录 
  39.     shutil.copy(sourceDir,  targetDir) 
  40.  
  41. def writeVersionInfo(targetDir):#往指定目录写文本文件 
  42.     open(targetDir, "wb").write("Revison:"
  43.  
  44. def getCurTime():#返回当前的日期,以便在创建指定目录的时候用 
  45.      nowTime = time.localtime() 
  46.      year = str(nowTime.tm_year) 
  47.      month = str(nowTime.tm_mon) 
  48.      if len(month) < 2
  49.          month = '0' + month 
  50.      day =  str(nowTime.tm_yday) 
  51.      if len(day) < 2
  52.          day = '0' + day 
  53.      return (year + '-' + month + '-' + day) 
  54.  
  55. if  __name__ =="__main__":#主函数 
  56.      print "Start(S) or Quilt(Q) \n" 
  57.      flag = True 
  58.      while (flag): 
  59.          answer = raw_input() 
  60.          if  answer == 'Q'
  61.              flag = False 
  62.          elif answer == 'S'
  63.              formatTime = getCurTime() 
  64.              targetFoldername = "Build " + formatTime + "-01" 
  65.              Target_File_Path += targetFoldername 
  66.              copyFiles(Debug_File_Path,   Target_File_Path) 
  67.              removeFileInFirstDir(Target_File_Path) 
  68.              coverFiles(Release_File_Path,  Target_File_Path) 
  69.              moveFileto(Firebird_File_Path,  Target_File_Path) 
  70.              moveFileto(AssistantGui_File_Path,  Target_File_Path) 
  71.              writeVersionInfo(Target_File_Path+"\test.txt"
  72.              print "all sucess" 
  73.          else
  74.              print "not the correct command" 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值