欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python3調(diào)用windows dos命令的例子

 更新時(shí)間:2019年08月14日 13:31:16   作者:褚金輝  
今天小編就為大家分享一篇python3調(diào)用windows dos命令的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

最近游戲項(xiàng)目在多個(gè)國家上線,每個(gè)國家都對應(yīng)兩份兒svn目錄(一份是本地策劃目錄,一份是線上目錄)。于是乎維護(hù)變得很煩躁。需要先更新本地策劃svn目錄,然后把更新的文件拷貝到對應(yīng)的線上目錄,然后提交線上svn目錄,然后維護(hù)服務(wù)器。多個(gè)國家就要重復(fù)多次類似的更新,拷貝,提交的操作,還要格外注意不能手抖,出現(xiàn)少復(fù)制的錯(cuò)誤。這種重復(fù)的操作很適合寫一個(gè)工具來完成。

于是考慮使用python來寫這個(gè)工具,最基本的操作就是使用python調(diào)用svn命令。因?yàn)閣indows安裝svn后是沒有svn命令行的,所以首先需要下載Apache-Subversion,這是svn命令行工具,解壓后設(shè)置環(huán)境變量。

python調(diào)用dos命令,使用下面的方法:

os.popen(command)

此方法會阻塞,直到執(zhí)行完成,當(dāng)然也會返回執(zhí)行的結(jié)果。如果你使用chcp設(shè)置過windows命令行的代碼頁,比如曾經(jīng)使用:chcp 65001。那么在執(zhí)行python程序的時(shí)候就可能產(chǎn)生gbk編碼的錯(cuò)誤,解決此問題需要重新使用:chcp 936來設(shè)置命令行代碼頁就OK了。

完整代碼如下:

import os
import shutil

'''

循環(huán)執(zhí)行每個(gè)一個(gè)資源目錄,每個(gè)目錄的執(zhí)行過程如下:

  1:更新線上目錄
  2:更新本地目錄
  3:把本地目錄的修改同步到線上目錄
  4:提交線上目錄

'''

#本地svn目錄
local_r_en = "server/trunk/resource"
local_r_ar = "server/resource_ar"
local_r_fr = "server/resource_fr"
local_r_ge = "server/resource_ge"
local_r_ita = "server/resource_ita"
local_r_ru = "server/resource_ru"
local_r_sp = "server/resource_sp"
local_r_tr = "server/resource_tr"
local_r_wp_en = "server/resource_wp"

#線上svn目錄
online_r_en = "serverOL/resource_en"
online_r_ar = "serverOL/resource_ar"
online_r_fr = "serverOL/resource_fr"
online_r_ge = "serverOL/resource_ge"
online_r_ita = "serverOL/resource_ita"
online_r_ru = "serverOL/resource_ru"
online_r_sp = "serverOL/resource_sp"
online_r_tk = "serverOL/resource_tk"
online_r_wp_en = "serverOL/resource_wp"


#不需要更新的服務(wù)器,請把下面對應(yīng)的資源目錄注釋掉
recource_dict = {}
recource_dict[local_r_en] = online_r_en       #美國
# recource_dict[local_r_ar] = online_r_ar      #阿拉伯
# recource_dict[local_r_fr] = online_r_fr      #法國
# recource_dict[local_r_ge] = online_r_ge      #德國
# recource_dict[local_r_ita] = online_r_ita     #意大利
# recource_dict[local_r_ru] = online_r_ru      #俄羅斯
# recource_dict[local_r_sp] = online_r_sp      #西班牙
# recource_dict[local_r_tr] = online_r_tk      #土耳其
# recource_dict[local_r_wp] = online_r_wp    #wp



'''
復(fù)制文件到目的文件
'''
def copyFile(src,dst):
  #目的目錄
  dst_dir = dst[:dst.rfind("/")]

  #如果目錄不存在則創(chuàng)建目錄,如果文件存在則刪除
  if not os.path.exists(dst_dir):
    os.makedirs(dst_dir)
  elif os.path.exists(dst):
    os.remove(dst)

  #復(fù)制新文件
  shutil.copyfile(src, dst)
#end method copyFile

'''
刪除文件
'''
def removeFile(dst):
  if os.path.exists(dst):
    os.remove(dst)
#end method removeFile


'''
打印命令結(jié)果
'''
def svnResult(result):
  for line in result:
    line = line.replace("\n","")
    print(line)
#end method svnResult


'''
svn命令執(zhí)行
'''
def svnSimpleExecute(command):
  print(">>",command)
  result = os.popen(command).readlines()
  svnResult(result)
#end method svnExecute


'''
svn提交目錄
'''
def svnCommit(dst):
  command = "svn status "+dst
  print(">>",command)
  command_result = os.popen(command).readlines()
  if len(command_result) <= 2:
    print("沒有需要添加,刪除和提交的內(nèi)容")
    return

  for line in command_result:
    #去掉換行符
    line = line.replace("\n","")
    print(line)
    line = line.replace(" ","")
    firstChar = line[0:1]
    filePath = line[1:]
    #命令
    command_add = "svn add "+filePath
    command_ci = "svn commit -m \"svn tools commit,更新維護(hù)。\" "+filePath
    command_rm = "svn delete "+filePath

    if firstChar == "?":#ADD命令
      svnSimpleExecute(command_add)
      svnSimpleExecute(command_ci)
    elif firstChar in "ADM":#COMMIT 命令
      svnSimpleExecute(command_ci)
    elif firstChar == "!": #刪除命令
      svnSimpleExecute(command_rm)
      svnSimpleExecute(command_ci)
    else:
      print("Unkonw.......",line)
#end method svnCommit


'''
解析獲得對應(yīng)的本地文件和線上文件路徑
'''
def parseLine(line,local_dir,online_dir):
  line = line.replace("'","")
  line = line.replace("\\","/")
  line = line.replace(local_dir,"")
  pos = line.find("/")
  return local_dir + line[pos:],online_dir + line[pos:]
#end method parseLine


#循環(huán)資源目錄
for local_dir in recource_dict:
  online_dir = recource_dict[local_dir]

  #更新線上資源
  command_online = "svn up " + online_dir
  svnSimpleExecute(command_online)

  #更新本地資源
  command_local = "svn up " + local_dir
  print(">>",command_local)

  lines = os.popen(command_local).readlines()
  if len(lines) <= 2:
    print("沒有更新內(nèi)容")
    continue

  #去掉頭和尾部內(nèi)容
  lines.pop(-1)
  lines.pop(0)

  for line in lines:
    #去掉換行符
    line = line.replace("\n","")
    print(line)
    #解析文件對應(yīng)目錄
    localFilePath,onlineFilePath = parseLine(line,local_dir,online_dir)

    if line.startswith("Restored") or line.startswith("A") or line.startswith("U"):
      print("復(fù)制文件:",localFilePath,"到",onlineFilePath) 
      copyFile(localFilePath,onlineFilePath)
    elif line.startswith("D"):
      #刪除
      print("刪除文件:",onlineFilePath)
      removeFile(onlineFilePath)

  #提交線上目錄
  svnCommit(online_dir)

這下維護(hù)起來就簡單多了,世界瞬間清凈了。

以上這篇python3調(diào)用windows dos命令的例子就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python中python-nmap模塊的使用介紹

    Python中python-nmap模塊的使用介紹

    這篇文章主要介紹了Python中python-nmap模塊的使用,主要是portScanner()類方法展開全文,portScanner()類用于實(shí)現(xiàn)對指定主機(jī)進(jìn)行端口掃描,更多介紹內(nèi)容,需要的朋友可以參考一下
    2022-02-02
  • Python常用小技巧總結(jié)

    Python常用小技巧總結(jié)

    這篇文章主要介紹了Python常用小技巧,實(shí)例總結(jié)了Python關(guān)于字典、字符串、隨機(jī)數(shù)等操作技巧,非常簡單實(shí)用,需要的朋友可以參考下
    2015-06-06
  • 讓python在hadoop上跑起來

    讓python在hadoop上跑起來

    讓python在hadoop上跑起來,python如何在hadoop上跑起來?感興趣的小伙伴們可以參考一下
    2016-01-01
  • 如何在python中使用selenium的示例

    如何在python中使用selenium的示例

    這篇文章主要介紹了如何在python中使用selenium的示例,selenium提供了一個(gè)通用的接口,可模擬用戶來操作瀏覽器,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • 詳解使用pymysql在python中對mysql的增刪改查操作(綜合)

    詳解使用pymysql在python中對mysql的增刪改查操作(綜合)

    本篇文章主要介紹了使用pymysql在python中對mysql的增刪改查操作,通過pymysql向數(shù)據(jù)庫進(jìn)行查刪增改,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-01-01
  • 詳解python實(shí)現(xiàn)線程安全的單例模式

    詳解python實(shí)現(xiàn)線程安全的單例模式

    這篇文章主要介紹了python實(shí)現(xiàn)線程安全的單例模式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • python爬蟲實(shí)現(xiàn)爬取同一個(gè)網(wǎng)站的多頁數(shù)據(jù)的實(shí)例講解

    python爬蟲實(shí)現(xiàn)爬取同一個(gè)網(wǎng)站的多頁數(shù)據(jù)的實(shí)例講解

    在本篇文章里小編給大家整理了一篇關(guān)于python爬蟲實(shí)現(xiàn)爬取同一個(gè)網(wǎng)站的多頁數(shù)據(jù)的實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-01-01
  • 解決python3 Pycharm上連接數(shù)據(jù)庫時(shí)報(bào)錯(cuò)的問題

    解決python3 Pycharm上連接數(shù)據(jù)庫時(shí)報(bào)錯(cuò)的問題

    今天小編就為大家分享一篇解決python3 Pycharm上連接數(shù)據(jù)庫時(shí)報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • python模擬實(shí)現(xiàn)分發(fā)撲克牌

    python模擬實(shí)現(xiàn)分發(fā)撲克牌

    這篇文章主要為大家詳細(xì)介紹了python模擬實(shí)現(xiàn)分發(fā)撲克牌,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • Python報(bào)錯(cuò)AssertionError:can only test a child proc問題

    Python報(bào)錯(cuò)AssertionError:can only test a c

    這篇文章主要介紹了Python報(bào)錯(cuò)AssertionError:can only test a child proc問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09

最新評論