怎么在python3项目中调用dos命令

  介绍

怎么在python3项目中调用dos命令?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

<强> python调用dos命令,使用下面的方法:

os.popen(命令)

此方法会阻塞,直到执行完成,当然也会返回执行的结果。如果你使用chcp设置过windows命令行的代码页,比如曾经使用:65001。那么在执行python程序的时候就可能产生gbk编码的错误,解决此问题需要重新使用:chcp 936来设置命令行代码页就好了。

<强>完整代码如下:

import 操作系统   import  shutil      & # 39;& # 39;& # 39;      循环执行每个一个资源目录,每个目录的执行过程如下:      1:才能更新线上目录   2:才能更新本地目录   3:才能把本地目录的修改同步到线上目录   4:才能提交线上目录      & # 39;& # 39;& # 39;      #本地svn目录   时间=local_r_en “服务器/箱子/resource"   时间=local_r_ar “服务器/resource_ar"   时间=local_r_fr “服务器/resource_fr"   时间=local_r_ge “服务器/resource_ge"   时间=local_r_ita “服务器/resource_ita"   时间=local_r_ru “服务器/resource_ru"   时间=local_r_sp “服务器/resource_sp"   时间=local_r_tr “服务器/resource_tr"   时间=local_r_wp_en “服务器/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            & # 39;& # 39;& # 39;   复制文件到目的文件   & # 39;& # 39;& # 39;   dst def 拷贝文件(src):   #目才能的目录   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)   #最终获得method 拷贝文件      & # 39;& # 39;& # 39;   删除文件   & # 39;& # 39;& # 39;   def  removeFile (dst):   if 才能os.path.exists (dst):   ,,,os.remove (dst)   #最终获得method  removeFile         & # 39;& # 39;& # 39;   打印命令结果   & # 39;& # 39;& # 39;   def  svnResult(结果):   for 才能;line 结果:拷贝   ,,,line =, line.replace (“\ n",““)   ,,,print(线)   #最终获得method  svnResult         & # 39;& # 39;& # 39;   svn命令执行   & # 39;& # 39;& # 39;   def  svnSimpleExecute(命令):   打印才能(““祝辞,命令)   result 才能=,os.popen(命令). readlines ()   svnResult才能(结果)   #最终获得method  svnExecute         & # 39;& # 39;& # 39;   svn提交目录   & # 39;& # 39;& # 39;   def  svnCommit (dst):   command =,才能“svn  status “+ dst   打印才能(““祝辞,命令)   command_result 才能=,os.popen(命令). readlines ()   if 才能len (command_result), & lt;=, 2:   ,,,print(“没有需要添加,删除和提交的内容“)   ,才能返回      for 才能;line  command_result:拷贝   ,,,#去掉换行符   ,,,line =, line.replace (“\ n",““)   ,,,print(线)   ,,,line =, line.replace (,,,,,,)   ,,,firstChar =,行(0:1)   ,,,filePath =,行(1:)   ,,,#命令   ,,,command_add =,“svn  add “+ filePath   ,,,command_ci =,“svn  commit  -m  \“svn  tools 提交更新维护。\““+ filePath   ,,,command_rm =,“svn  delete “+ filePath      ,,,if  firstChar ==,“?“: #加命令   ,,,,,svnSimpleExecute (command_add)   ,,,,,svnSimpleExecute (command_ci)   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在python3项目中调用dos命令