您好,登錄后才能下訂單哦!
怎么在python3項目中調用dos命令?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
python調用dos命令,使用下面的方法:
os.popen(command)
此方法會阻塞,直到執行完成,當然也會返回執行的結果。如果你使用chcp設置過windows命令行的代碼頁,比如曾經使用:chcp 65001。那么在執行python程序的時候就可能產生gbk編碼的錯誤,解決此問題需要重新使用:chcp 936來設置命令行代碼頁就OK了。
完整代碼如下:
import os import shutil ''' 循環執行每個一個資源目錄,每個目錄的執行過程如下: 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" #不需要更新的服務器,請把下面對應的資源目錄注釋掉 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 ''' 復制文件到目的文件 ''' def copyFile(src,dst): #目的目錄 dst_dir = dst[:dst.rfind("/")] #如果目錄不存在則創建目錄,如果文件存在則刪除 if not os.path.exists(dst_dir): os.makedirs(dst_dir) elif os.path.exists(dst): os.remove(dst) #復制新文件 shutil.copyfile(src, dst) #end method copyFile ''' 刪除文件 ''' def removeFile(dst): if os.path.exists(dst): os.remove(dst) #end method removeFile ''' 打印命令結果 ''' def svnResult(result): for line in result: line = line.replace("\n","") print(line) #end method svnResult ''' svn命令執行 ''' 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("沒有需要添加,刪除和提交的內容") 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,更新維護。\" "+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 ''' 解析獲得對應的本地文件和線上文件路徑 ''' 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 #循環資源目錄 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("沒有更新內容") continue #去掉頭和尾部內容 lines.pop(-1) lines.pop(0) for line in lines: #去掉換行符 line = line.replace("\n","") print(line) #解析文件對應目錄 localFilePath,onlineFilePath = parseLine(line,local_dir,online_dir) if line.startswith("Restored") or line.startswith("A") or line.startswith("U"): print("復制文件:",localFilePath,"到",onlineFilePath) copyFile(localFilePath,onlineFilePath) elif line.startswith("D"): #刪除 print("刪除文件:",onlineFilePath) removeFile(onlineFilePath) #提交線上目錄 svnCommit(online_dir)
關于怎么在python3項目中調用dos命令問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。