Copy and delete files in Python OS. path and shutil modules

Source: Internet
Author: User

We mainly use the OS. path and shutil modules to copy and delete files. You can modify the relevant code as needed.

#! /Usr/bin/env python #-*-coding: UTF-8-*-import OS. path import shutil import time, datetime def copyFiles (sourceDir, targetDir): # copy all files in a directory to the specified directory if sourceDir. find (". svn ")> 0: return for file in OS. listdir (sourceDir): sourceFile = OS. path. join (sourceDir, file) targetFile = OS. path. join (targetDir, file) if OS. path. isfile (sourceFile): if not OS. path. exists (targetDir): OS. makedirs (tar GetDir) if not OS. path. exists (targetFile) or (OS. path. exists (targetFile) and (OS. path. getsize (targetFile )! = OS. path. getsize (sourceFile): open (targetFile, "wb "). write (open (sourceFile, "rb "). read () if OS. path. isdir (sourceFile): First_Directory = False copyFiles (sourceFile, targetFile) def removeFileInFirstDir (targetDir): # delete all files in the first-level directory for file in OS. listdir (targetDir): targetFile = OS. path. join (targetDir, file) if OS. path. isfile (targetFile): OS. remove (targetFile) def coverFiles (sourceDir, targetDir): # copy all files in the first-level directory to the specified directory for file in OS. listdir (sourceDir): sourceFile = OS. path. join (sourceDir, file) targetFile = OS. path. join (targetDir, file) # cover the files if OS. path. isfile (sourceFile): open (targetFile, "wb "). write (open (sourceFile, "rb "). read () def moveFileto (sourceDir, targetDir): # copy the specified file to the shutil directory. copy (sourceDir, targetDir) def writeVersionInfo (targetDir): # write the text file open (targetDir, "wb") to the specified directory "). write ("Revison:") def getCurTime (): # returns the current date to use nowTime = time when creating the specified directory. localtime () year = str (nowTime. tm_year) month = str (nowTime. tm_mon) if len (month) <2: month = '0' + month day = str (nowTime. tm_yday) if len (day) <2: day = '0' + day return (year + '-' + month + '-' + day) if _ name _ = "_ main _": # main Function print "Start (S) or Quilt (Q) \ n" flag = True while (flag): answer = raw_input () if answer = 'q': flag = False elif answer = 's': formatTime = getCurTime () targetFoldername = "Build" + formatTime + "-01" Target_File_Path + = targetFoldername copyFiles) moveFileto (AssistantGui_File_Path, Target_File_Path) writeVersionInfo (Target_File_Path + "\ test.txt") print "all sucess" else: print "not the correct command"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.