Python Copy file Operation example detailed

Source: Internet
Author: User
The examples in this article describe the python copy file operation usage. Share to everyone for your reference, as follows:

Here, Python implements a small, automated version of the tool. This "Automatic Version" is a bit virtual, just simply copy the configuration file under the Debug directory to the specified directory, copy the generated files under release to the same designation, filter out the unwanted folders (. svn), and then add several specific files to the specified directory.

This is my first Python applet.

Here's a look at the implementation of the Code.

First insert the necessary libraries:

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

Then there's a whole bunch of function 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 files 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 from 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 the specified directory:

def writeversioninfo (targetDir):   open (TargetDir, "WB"). Write ("Revison:")

Returns the current date to be used 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 there is the implementation of the main function:

if __name__ = = "__main__":    print "Start (S) or Quilt (Q) \ n"    flag = True while    (flag):      answer = raw_input ()  C4/>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 is helpful for Python program design.

  • 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.