Details about how to copy a file using Python

Source: Internet
Author: User
This article describes how to copy and delete files in Python. It involves Python's skills for copying and deleting files and directories. It has some reference value, for more information about how to copy Python files, see the following example. We will share this with you for your reference. The details are as follows:

Python is used to implement a small automatic release tool. This "Auto Release Version" is a bit Virtual. It simply copies the configuration files under the debug directory to the specified directory and copies the generated files under the Release to the same specified directory, filter out unnecessary folders (. svn), and then add several specific files to the specified directory.

This is my first python applet.

The following describes the implementation of the Code.

First, insert the necessary database:

import os import os.path import shutil import time, datetime

Then there are a lot of functional functions. The first one is to copy all the files in a directory to the specified directory:

def copyFiles(sourceDir, targetDir):    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(targetDir)        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)

Delete all objects in the first-level directory:

def removeFileInFirstDir(targetDir):    for file in os.listdir(targetDir):      targetFile = os.path.join(targetDir, file)      if os.path.isfile(targetFile):        os.remove(targetFile)

Copy all files in the first-level directory to the specified directory:

def coverFiles(sourceDir, targetDir):      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())

Copy the specified file to the directory:

def moveFileto(sourceDir, targetDir):   shutil.copy(sourceDir, targetDir)

Write a text file to a specified directory:

def writeVersionInfo(targetDir):   open(targetDir, "wb").write("Revison:")

Returns the current date to use when creating the specified directory:

def getCurTime():    nowTime = time.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)

Then the main function is implemented:

if __name__ =="__main__":    print "Start(S) or Quilt(Q) \n"    flag = True    while (flag):      answer = raw_input()      if 'Q' == answer:        flag = False      elif 'S'== answer :        formatTime = getCurTime()        targetFoldername = "Build " + formatTime + "-01"        Target_File_Path += targetFoldername       copyFiles(Debug_File_Path,  Target_File_Path)        removeFileInFirstDir(Target_File_Path)        coverFiles(Release_File_Path, Target_File_Path)        moveFileto(Firebird_File_Path, Target_File_Path)        moveFileto(AssistantGui_File_Path, Target_File_Path)        writeVersionInfo(Target_File_Path+"\\ReadMe.txt")        print "all sucess"      else:        print "not the correct command"

I hope this article will help you with python programming.

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.