python 文件夹与文件操作实例
正则表达式来清理文件夹
import os
import sys
import re
import shutil
def cleanup(dir, regrex, num):
if not os.path.exists(dir) and not os.path.isdir(dir) :
print 'path %s is not existed or is not a directory' %dir
return false
subfolderdict = {}
for subi in os.listdir(dir):
sf = os.path.join(dir,subi)
if os.path.isdir(sf) and not re.match(regrex, subi) == none:
sftime = os.path.getctime(sf)
subfolderdict[sftime] = sf
else:
continue
subfolders = subfolderdict.keys()
if len(subfolders) == 0 : return true
subfolders.sort(reverse=true)
n = int(num)
if len(subfolders) >= n :
subfolders = subfolders[n:]
else: return true
if len(subfolders) == 0 : return true
for sftime in subfolders:
sf = subfolderdict[sftime]
#shutil.rmtree(sf)
print '%s is removed' % sf
return true
def usage():
usage = 'n
function:n
clean up subfolders in (dir), as a result :n
just keep the subfolders which are matched with (regrex), n
and the number of the subfoler cannot more then (num).n
usage:n
python %s dir regrex numn
' %sys.argv[0]
print usage
if __name__ == '__main__':
if len(sys.argv) == 4 :
cleanup(sys.argv[1],sys.argv[2],sys.argv[3])
else:
usage()
python subst删除文件夹
代码:
import os
import sys
import shutil
import subprocess
def runcommand(cmd):
return subprocess.call(cmd)
def substdriveforpath(drive, path):
substcmd = "subst" + " " + drive + " " + path
return runcommand(substcmd)
def unsubstdriveforpath(drive):
unsubstcmd = "subst" + " " +drive + " " + "/d"
runcommand(unsubstcmd)
def autosubst(path):
result = false
useddrive = ""
for drive in "zyxwvutsrqponmlkjihgfedcba":
fulldrive = drive + ":"
returncode = substdriveforpath(fulldrive,path)
if(returncode == 0):
useddrive = fulldrive
break
if(not useddrive == ""):
result = true
return result, useddrive
def rmdirwithsubst(dir, subst = true):
if not os.path.exists(dir) and not os.path.isdir(dir) :
print 'path %s is not existed or is not a directory' %dir
return false
if subst:
parent, curdir = os.path.split(dir)
re, d = autosubst(parent)
if re == false :
print 'subst failed'
return false
dir = d + '' + curdir
shutil.rmtree(dir)
unsubstdriveforpath(d)
return true
def usage():
usage = '
when the dirctory cannot be removed in which some files path are too long.n
usage: python %s folder1 [folder2 ...]n' %sys.argv[0]
print usage
if __name__ == '__main__':
if len(sys.argv) < 2 : usage()
for folder in sys.argv[1:]:
rmdirwithsubst(folder)
文件复制
1. 配置文件 (mojo.ini)
[main]
skin_path=e:accentrix.srcsunridersrcwebdataskins
mojo_templates=sunriderreplicator_skin_1,sunriderreplicator_skin_2,sunriderreplicator_skin_3,sunriderreplicator_skin_4
file_path=layout.master,images1.png
[target]
path=z:dataskins
--在这里解释一下:
[main] 源路径的配置
skin_path:这个是mojo皮肤的目录位置
mojo_templates:模板目录用逗号分开表示多个,(其实也可以用其它的目录)
file_path:模板下面对应的文件,用逗号分开,也可以包含子目录(eg: imagesimagek.jpg)
[target] 要复制到的位置
path:就是mojo皮肤所在的位置,参考main节点下面的 skin_path就可以了
--以下就是py的代码了
import os;
import configparser;
import shutil;
def mojoupdate():
print "application will start to update"
path=".mojo.ini"
main="main"
config=configparser.configparser();
config.read(path)
skinpath=config.get(main,"skin_path")
for _templatedirectory in config.get(main,"mojo_templates").split(","):
sourcefolderpath=skinpath+""+_templatedirectory
for _file in config.get(main,"file_path").split(","):
dest_path=config.get("target","path")+""+_templatedirectory
dest=dest_path+""+_file
fullpath=sourcefolderpath+ "" +_file
print fullpath
if os.path.exists(fullpath):
print "copy file "+fullpath+" to " + dest
createfolder(os.path.split(dest)[0])
shutil.copyfile(fullpath,dest)
else:
print fullpath +" is not exists"
print "copy completed"
#-----------------------------------------------------
#recursion create folder
def createfolder(path):
if os.path.exists(path)==false:
createfolder(os.path.split(path)[0])
os.mkdir(path)
-------------------------
运行mojoupdate()这个方法即可