欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

接触Python编写小脚本来辅助项目生成(代码实例)

程序员文章站 2022-04-20 09:08:48
文件夹是否存在 os.path.isdir() 获取工作路径 os.getcwd() 定义list list=[] Try语句使用...

文件夹是否存在

 os.path.isdir()

获取工作路径

 os.getcwd()

定义list

 list=[]

Try语句使用

         try:
              os.remove(fullPath)
         except IOError :
              print("Try deleted file  "+i+" failed")

代码

import os, sys, shutil
path_list=os.listdir(os.getcwd())
releasePath="./Bin/Release/"
dotfuscatedPath=releasePath+"Dotfuscated/"
removeList=[]
releaseList=[]
def reMakerelease():
 print("目录为: %s"%path_list)
#1
 if os.path.isdir(releasePath):
  if os.path.isdir(dotfuscatedPath):
      for i in removeList:
          fullPath=releasePath+i
          print(fullPath)
          if os.path.isfile(fullPath):
            try:
              os.remove(fullPath)
            except IOError :
                print("Try deleted file  "+i+" failed")
                pass
          else:
              print("File "+i+" not found")
  else:
      print("Directory "+dotfuscatedPath+" not found")
 else:
     print("Directory "+releasePath+" not found")
#2
 if os.path.isdir(dotfuscatedPath):
     for i in releaseList:
         try:
           if ".exe" in i:
              shutil.copyfile(dotfuscatedPath+removeList[releaseList.index(i)],releasePath+i)
           else:
              shutil.copyfile(dotfuscatedPath+releaseList[releaseList.index(i)],releasePath+i)
         except IOError :
             print("Try Copy file  "+i+" failed")
             pass
 else:
     print("Directory "+dotfuscatedPath+" not found")

 #3
 deleteDir();
def getRemoveList():
 removeList.append("Svr.exe")
 removeList.append("Archivist.exe")
 removeList.append("Admin.exe")
 removeList.append("Client.exe")
 removeList.append("Compere.exe")
 removeList.append("Documenter.exe")
 print(removeList)
def getReleaseList():
 releaseList.append("服务器.exe")
 releaseList.append("归档查询员.exe")
 releaseList.append("管理员.exe")
 releaseList.append("客户端.exe")
 releaseList.append("会议管理员.exe")
 releaseList.append("资料员.exe")
 releaseList.append("UserControlsLB.dll")
 releaseList.append("Env.dll")
 releaseList.append("FCN.dll")
 releaseList.append("I386.dll")
 releaseList.append("LicInfo.dll")

def deleteDir():
 if os.path.isdir(dotfuscatedPath):
  shutil.rmtree(dotfuscatedPath)

def main():
    getRemoveList();
    getReleaseList();
    reMakerelease();
    print("Command executed")
    pass
if __name__ == '__main__':
    main()