用python实现了一个小型的自动发版本的工具。这个“自动发版本”有点虚, 只是简单地把debug 目录下的配置文件复制到指定目录,把Release下的生成文件复制到同一指定,过滤掉不需要的文件夹(.svn),然后再往这个指定目录添加几个特定的文件。

这个是我的第一个python小程序。

下面就来看其代码的实现。

首先插入必要的库:

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

然后就是一大堆功能函数。第一个就是把某一目录下的所有文件复制到指定目录中:


 def copyFiles(sourceDir,  targetDir):
if sourceDir.find(".svn") >:
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)

删除一级目录下的所有文件:

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

复制一级目录下的所有文件到指定目录:


 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())

复制指定文件到目录:

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

往指定目录写文本文件:

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

返回当前的日期,以便在创建指定目录的时候用:


 def getCurTime():
nowTime = time.localtime()
year = str(nowTime.tm_year)
month = str(nowTime.tm_mon)
if len(month) <:
month =''+ month
day = str(nowTime.tm_yday)
if len(day) <:
day =''+ day
return (year +'-'+ month +'-'+ day)

然后就是主函数的实现了:


 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"

感觉是果然简单, 不过简单的原因是因为库函数丰富,语言基本特性的简单真没感觉出来。

python 文件拷贝的更多相关文章

  1. windows python文件拷贝到linux上执行问题

    之前在Windows下写好了一个Python脚本,运行没问题,今天在Linux下,脚本开头的注释行已经指明了解释器的路径,也用chmod给了执行权限,但就是不能直接运行脚本. 1 问题1: 报错:: ...

  2. windows python文件拷贝到linux上执行问题-换行符问题/r/n

    之前在Windows下写好了一个Python脚本,运行没问题,今天在Linux下,脚本开头的注释行已经指明了解释器的路径,也用chmod给了执行权限,但就是不能直接运行脚本. 1 问题1: 报错:: ...

  3. python 简单实现文件拷贝

    1.背景 一日加班需要写一个文件拷贝的函数. 写了几版拷贝函数,有需要的直接粘贴过去 def CopyLocaleFile1(sorfile,desfile): #第一版 sorfp=open(sor ...

  4. python批量拷贝文件

    普通批量拷贝文件 import os import shutil import logging from logging import handlers from colorama import Fo ...

  5. Python:tarxjb简单、安全文件拷贝、传输

    tarxjb 简单.安全文件拷贝.传输 描述 通过python paramiko库实现简易ssh.sftp执行操作,从而实现文件的远程传输 Github 优点: 可靠传输,文件不易受损 安全传输,避免 ...

  6. python文件处理-根据txt列表将文件从其他文件夹 拷贝到指定目录

    内容涉及:路径拼接,文件拷贝,内容追加(append) # !/usr/bin/python # -*- coding: UTF-8 -*- import pandas as pd import os ...

  7. Python 文件操作函数

    这个博客是 Building powerful image classification models using very little data 的前期准备,用于把图片数据按照教程指示放到规定的文 ...

  8. linux或者windows下的文件拷贝

    #  上代码 #!/usr/bin/env python # -*- coding:utf-8 -*- import os import shutil import tarfile base_dir ...

  9. python文件和元组

    python文件操作 相较于java,Python里的文件操作简单了很多 python 获取当前文件所在的文件夹: os.path.dirname(__file__) 写了一个工具类,用来在当前文件夹 ...

随机推荐

  1. 034. asp.netWeb用户控件之三通过用户控件实现用户注册和登录

    用户控件login.ascx代码: <%@ Control Language="C#" AutoEventWireup="true" CodeFile=& ...

  2. Docker之功能汇总

    Docker-给容器做端口映射 基本的命令是 -P(大写) :Docker 会随机映射一个 49000~49900 的端口到内部容器开放的网络端口基本的命令是 -p(小写) :Docker则可以指定要 ...

  3. .NET Core On Mac 第一步,配置环境

    话说.NET Core出来这么久了,也没搞搞,实在是羞愧难当啊.既然选择了开始那就不能半途而废啊..NET 跨平台了,那我就用Mac试试吧. 安装步骤一:安装Homebrew 其中会遇到权限不够的问题 ...

  4. 什么是 Web API

    http://www.cnblogs.com/developersupport/p/aspnet-webapi.html Web API 强势入门指南 Web API是一个比较宽泛的概念.这里我们提到 ...

  5. eclipse下新建maven项目

    eclipse下新建maven项目 1.1下载maven安装包 进入Maven官网的下载页面:http://maven.apache.org/download.cgi,如下图所示:\ 选择当前最新版本 ...

  6. Sublime Text 3 插件安装及Vim 模式设置

    1.安装Sublime Text 3  下载安装:http://www.sublimetext.com/3 Package Control安装:https://sublime.wbond.net/in ...

  7. HBase Java API类介绍

    几个相关类与HBase数据模型之间的对应关系 java类 HBase数据模型 HBaseAdmin 数据库(DataBase) HBaseConfiguration HTable 表(Table) H ...

  8. 如何把项目部署到OSChina上

    1. 在苹果电脑终端   ls -la  查看当前目录所有的隐藏文件 2. 删除 .ssh文件  rm -rf .ssh 3.创建一个隐藏的文件  mkdir .ssh   在查看 4.进入 .ssh ...

  9. 【VB.NET】类绑定控件,实现文本框快捷键全选

    Public Class KeyBinder Public Sub BindControl(ByRef CControl As TextBox) AddHandler CControl.KeyDown ...

  10. QBC分页查询

    1.第一种方式 public class DetailDaoImpl extends HibernateTemplate implements DetailDaoInterface { private ...