十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
怎么在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命令问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。