Python implements file copy, delete

Source: Internet
Author: User
Tags file copy

A small, automated version of the tool was implemented in Python. 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:

1 Import OS
2 Import Os.path
3 Import Shutil
4 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:

1 def copyfiles (SourceDir, TargetDir):
2 ifSourcedir.find (". SVN") >0:
3 return
4 forfileinchOs.listdir (SourceDir):
5 sourcefile=os.path.join (sourcedir, file)
6 TargetFile=os.path.join (targetDir, file)
7 ifOs.path.isfile (sourcefile):
8 ifNot os.path.exists (targetDir):
9 Os.makedirs (TargetDir)
Ten ifNot os.path.exists (targetfile) or (Os.path.exists (targetfile) and (Os.path.getsize (targetfile)!=os.path.getsize (sourcefile)):
One Open (TargetFile,"WB"). Write (Open (sourcefile,"RB"). Read ())
A ifOs.path.isdir (sourcefile):
- first_directory=False
- CopyFiles (sourcefile, targetfile)

Delete all files in the first level directory:

1 def removefileinfirstdir (TargetDir):
2 forfileinchOs.listdir (TargetDir):
3 TargetFile=Os.path.join (targetDir, file)
4 ifOs.path.isfile (targetfile):
5 Os.remove (targetfile)

Copy all files from the first level directory to the specified directory:

1 def coverfiles (SourceDir, TargetDir):
2 forfileinchOs.listdir (SourceDir):
3 sourcefile=os.path.join (sourcedir, file)
4 TargetFile=os.path.join (targetDir, file)
5 #cover The files
6 ifOs.path.isfile (sourcefile):
7 Open (TargetFile,"WB"). Write (Open (sourcefile,"RB"). Read ())

Copy the specified file to the directory:

1 def movefileto (SourceDir, TargetDir):
2 shutil.copy (SourceDir, TargetDir)

Write a text file to the specified directory:

1 def writeversioninfo (TargetDir):
2 Open (TargetDir,"WB"). Write ("Revison:")

Returns the current date to be used when creating the specified directory:

1 def getcurtime ():
2 Nowtime=Time.localtime ()
3 Year=Str (nowtime.tm_year)
4 Month=Str (Nowtime.tm_mon)
5 ifLen (month)<2:
6 Month='0'+Month
7 Day=Str (nowtime.tm_yday)
8 ifLen (Day)<2:
9 Day='0'+Day
Ten return( Year+'-'+Month+'-'+Day )

Then there is the implementation of the main function:

1 if__name__=="__main__":
2 Print"Start (S) or Quilt (Q) \ n"
3 Flag=True
4 while(flag):
5 Answer=Raw_input ()
6 if'Q'==Answer:
7 Flag=False
8 elif'S'==Answer:
9 Formattime=Getcurtime ()
Ten Targetfoldername="Build"+Formattime+"-01"
One Target_file_path+=Targetfoldername
A
- CopyFiles (Debug_file_path, Target_file_path)
- Removefileinfirstdir (Target_file_path)
the 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"

The feeling is simple, but the simple reason is because the library function is rich, the basic characteristics of the simple language really do not feel out.

Python implements file copy, delete

Related Article

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.