Python复制文件操作实例详解
作者:一克棉花 发布时间:2023-10-22 19:15:29
标签:Python,复制,文件
本文实例讲述了Python复制文件操作用法。分享给大家供大家参考,具体如下:
这里用python实现了一个小型的自动发版本的工具。这个“自动发版本”有点虚, 只是简单地把debug 目录下的配置文件复制到指定目录,把Release下的生成文件复制到同一指定,过滤掉不需要的文件夹(.svn),然后再往这个指定目录添加几个特定的文件。
这个是我的第一个python小程序。
下面就来看其代码的实现。
首先插入必要的库:
import os
import os.path
import shutil
import time, datetime
然后就是一大堆功能函数。第一个就是把某一目录下的所有文件复制到指定目录中:
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)
删除一级目录下的所有文件:
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) < 2:
month = '0' + month
day = str(nowTime.tm_yday)
if len(day) < 2:
day = '0' + 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程序设计有所帮助。


猜你喜欢
- Rex 是 Perl 编写的基于 SSH 链接的集群配置管理系统,语法上类似 Puppet DSL。官网中文版见 http://rex.pe
- 运维平台导入数据这一功能实在是太重要了,我敢说在没有建自己的cmdb平台前,大多数公司管理服务器信息肯定是表格,用表格最麻烦的就是有点更新就
- 写过一篇"正则表达式30分钟入门教程",有读者问:[^abc]表示不包含a、b、c中任意字符, 我想实现不包含字符串ab
- 前言:分区是一种表的设计模式,通俗地讲表分区是将一大表,根据条件分割成若干个小表。但是对于应用程序来讲,分区的表和没有分区的表是一样的。换句
- 在Python语言中,json数据与dict字典以及对象之间的转化,是必不可少的操作。在Python中自带json库。通过import js
- pytorch定义新的自动求导函数在pytorch中想自定义求导函数,通过实现torch.autograd.Function并重写forwa
- 本文实例讲述了JS实现动画兼容性的transition和transform方法。分享给大家供大家参考,具体如下:今天在开发纯手工js打造图片
- 在上一篇的基础上,继续在透明窗体上绘制小球,一、画个大球看看(一)核心代码在on_resize函数内部增加如下画圆的代码 can
- 首先:文章用到的解析库介绍BeautifulSoup:Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修
- 本篇文章主要是由于计划使用django写一个计划任务出来,可以定时的轮换值班人员名称或者定时执行脚本等功能,百度无数坑之后,终于可以凑合把这
- 本文实例讲述了JS实现淡入淡出图片效果的方法。分享给大家供大家参考,具体如下:效果:鼠标移入时,图片由半透明逐渐变成清晰,移出时,由清晰变为
- 前言:循环中通过break语句会立刻终止并跳出循环语句。break就像是终止按键,不管执行到哪一步,只要遇到break,不管什么后续步骤,直
- 1.安装完成自动化测试,需要配置三个东西。selenium:pip就可以了chrome:浏览器下载一个谷歌浏览器就行chrome-drive
- 如下所示:import numpy as npfrom torchvision.transforms import Compose, ToT
- 一.使用库说明Golang中连接kafka可以使用第三方库:github.com/Shopify/sarama二.Kafka Produce
- 解析url用的类库:python2版本: from urlparse import urlparseimport urllibpython3
- opencv-python打开USB或笔记本前置摄像头代码其中video_index是摄像头编号,一般前置摄像头为0,USB摄像头为1或2.
- 背景说起Mysql死锁,之前写过一次有关Mysql加锁的基本介绍,对于一些基本的Mysql锁或者死锁都有一个简单的认识,可以看下这篇文章为什
- 一.图像几何变换图像几何变换不改变图像的像素值,在图像平面上进行像素变换。适当的几何变换可以最大程度地消除由于成像角度、透视关系乃至镜头自身
- 我有一个2D(二维) NumPy数组,并希望用255.0替换大于或等于阈值T的所有值。据我所知,最基础的方法是:shape = arr.sh