文件目录操作
#-*- coding: utf-8 -*-from __future__ import with_statement
import platform
import os
import shutil
import stat
import ConfigParser
#check the type of system
def CheckSys():
sysname=platform.system()
if sysname=="Windows":
return "win"
elif sysname=="Linux" :
return "lin"
else :
return "other"
#check the type of path_filename
def Check_file_or_dir(path_filename):
#if not path_filename:
# path_filename=os.getcwd()
exist = os.path.exists(path_filename)
if not exist:
print path_filename,"is not exist"
return
if os.path.isfile(path_filename):
return "file"
elif os.path.isdir(path_filename):
return "dir"
elif os.path.islink(path_filename):
return "link"
else:
return "other"
#cd path
def CdDir(path):
exist = os.path.exists(path)
if not exist and not os.path.isdir(path):
print path,"is not exist"
return
else:
os.chdir(path)
#create the file or dir
def CreateFileDir(path_filename,isdir):
exist = os.path.exists(path_filename)
if exist:
print path_filename,"is Already exist"
return
else:
if isdir:
os.mkdir(path_filename)
else:
f=open(path_filename,'w')
f.close()
return path_filename
#delete file or dir
def DeleteFileDir(path_filename):
exist = os.path.exists(path_filename)
filedir=Check_file_or_dir(path_filename)
if exist :
if filedir=="dir":
shutil.rmtree(path_filename)
elif filedir=="file":
os.remove(path_filename)
elif filedir=="link" :
pass
else:
print path_filename,"is not Exist!"
return
#copy the file or dir
def CopyFileDir(oldpath_filename,newpath_filename):
exist = os.path.exists(oldpath_filename)
filedir=Check_file_or_dir(oldpath_filename)
if not exist:
print oldpath_filename,"is not exist"
return
if filedir=="file":
shutil.copyfile(oldpath_filename,newpath_filename)
elif filedir=="dir" :
CreateFileDir(newpath_filename,True)
for filename in os.listdir(oldpath_filename):
sourceFile = os.path.join(oldpath_filename,filename)
targetFile = os.path.join(newpath_filename,filename)
#cover the files
if os.path.isfile(sourceFile):
open(targetFile, "wb").write(open(sourceFile, "rb").read())
else:
CopyFileDir(sourceFile,targetFile)
#rename the file or dir
def RenameFileDir(oldpath_filename,newpath_filename):
exist = os.path.exists(oldpath_filename)
if not exist:
print oldpath_filename,"is not exist"
return
os.rename(oldpath_filename,newpath_filename)
#list the files of file or dir
def ListfileOfdir(path_filename,listfile):
exist = os.path.exists(path_filename)
if not exist :
print path_filename,"is not exist"
return
name=Check_file_or_dir(path_filename)
if not path_filename:
return
else:
if name=="file":
listfile.append(path_filename)
elif name=="dir":
for item in os.listdir(path_filename):
itemsrc = os.path.join(path_filename, item)
ListfileOfdir(itemsrc,listfile)
return listfile
#change the mod of the file or dirAndfiles,but Record the old mode of the file or dirAndfile
def RecordChangeFileDirMode(path_filename,mode=0,modefile="FiledirMode.cfg"):
exist = os.path.exists(path_filename)
pathcfg= os.path.join(os.getcwd(), modefile)
if not os.path.exists(pathcfg):
f=open(pathcfg,'w')
f.close()
if not os.path.exists(path_filename):
print path_filename,"is not exsit"
return
filedir=Check_file_or_dir(path_filename)
config=ConfigParser.ConfigParser()
with open(pathcfg,"w") as cfgfile:
config.add_section("modeinfo")
if filedir=="dir":
for root, dirs, files in os.walk(path_filename):
print root
fileStats = os.stat (root)
filemode=fileStats[stat.ST_MODE]
permiss=stat.S_IMODE(filemode)
oldmode= oct(permiss)
pathfilename=root.replace(':','?')
config.set("modeinfo",pathfilename,oldmode)
os.chmod(root, permiss & 0 )
os.chmod(root, int(mode) )
for filename in files:
fp=os.path.join(root, filename)
fileStats = os.stat (fp)
filemode=fileStats[stat.ST_MODE]
permiss=stat.S_IMODE(filemode)
oldmode= oct(permiss)
#status, output = commands.getstatusoutput('chmod 666 /home/chen')
#print oct(stat.S_IMODE(filestats[stat.ST_MODE]))
pathfilename=fp.replace(':','?')
config.set("modeinfo",pathfilename,oldmode)
os.chmod(fp, permiss & 0 )
os.chmod(fp, int(mode) )
elif filedir=="file":
fileStats = os.stat (path_filename)
filemode=fileStats[stat.ST_MODE]
permiss=stat.S_IMODE(filemode)
oldmode= oct(permiss)
pathfilename=path_filename.replace(':','?')
config.set("modeinfo",pathfilename,oldmode)
os.chmod(path_filename, permiss & 0 )
os.chmod(path_filename, int(mode) )
config.write(cfgfile)
#change the mode of the file or dirAndfiles,but don't Record the old mode of the file or dirAndfile
def ChangeMod(path_filename,mode=0):
if not os.path.exists(path_filename):
print path_filename,"is not exsit"
return
filedir=Check_file_or_dir(path_filename)
if filedir=="dir":
for root, dirs, files in os.walk(path_filename):
#print root
rootstats=os.stat(root)
os.chmod(root, stat.S_IMODE(rootstats[stat.ST_MODE]) & 0 )
os.chmod(root, int(mode))
for filename in files:
#print filename
fp=os.path.join(root, filename)
#status, output = commands.getstatusoutput('chmod 666 /home/chen')
filestats=os.stat(fp)
#print oct(stat.S_IMODE(filestats[stat.ST_MODE]))
os.chmod(fp, stat.S_IMODE(filestats[stat.ST_MODE]) & 0 )
os.chmod(fp, int(mode))
elif filedir=="file":
os.chmod(path_filename, stat.S_IMODE(filestats[stat.ST_MODE]) & 0 )
os.chmod(path_filename, int(mode))
#only chang mode of the file or dir
def ChangefileMod(path_filename,mode=0):
if not os.path.exists(path_filename):
print path_filename,"is not exsit"
return
rootstats=os.stat(path_filename)
os.chmod(path_filename, stat.S_IMODE(rootstats[stat.ST_MODE]) & 0 )
os.chmod(path_filename, int(mode))
#read the old mode of the file or dir
def ReadFileDirMode(path_filename,modefile="FiledirMode.cfg"):
if not os.path.exists(path_filename):
print path_filename,"is not exsit"
return
pathcfg= os.path.join(os.getcwd(), modefile)
if not os.path.exists(pathcfg):
print modefile,"is not exsit"
config=ConfigParser.ConfigParser()
with open(pathcfg,"r") as cfgfile:
config.readfp(cfgfile)
pathfilename=path_filename.replace(':','?')
mode=config.get("modeinfo",pathfilename)
return mode
#read Current mode of the file or dir
def ReadCurfiledirMode(path_filename):
if not os.path.exists(path_filename):
print path_filename,"is not exsit"
return
fileStats = os.stat (path_filename)
filemode=fileStats[stat.ST_MODE]
permiss=stat.S_IMODE(filemode)
curmode= oct(permiss)
print curmode
return curmode
#recover the mode of the file or dir
def RecoverFileDirMode(path_filename,modefile="FiledirMode.cfg"):
mode=ReadFileDirMode(path_filename,modefile)
print mode
ChangefileMod(path_filename,mode)
if __name__ == "__main__":
#CreateFileDir("e:\\file2",True)
#listfile=[]
#ChangeMod(r"e:\file\f",r"e:\file\book")
#CdDir(r"e:\file\book")
#print os.getcwd()
#RecordChangeFileDirMode(r"E:\test",0666)
#print ReadFileDirMode(r"E:\test\hello\app.yaml")
#myfile_stat = os.stat(r"E:\test\hello\app.yaml")
#size1 = myfile_stat[stat.ST_SIZE]
#mode1 = myfile_stat[stat.ST_MODE]
#print size1 ,mode1
#print listfile
ChangefileMod(r"E:\test\hello\app.yaml",0444)
ReadCurfiledirMode(r"E:\test\hello\app.yaml")
RecoverFileDirMode(r"E:\test\hello\app.yaml")
ReadCurfiledirMode(r"E:\test\hello\app.yaml")