如何从python执行命令提示符命令

时间:2011-03-30 13:10:04

标签: python windows

我试过这样的事情,但没有效果:

command = "cmd.exe"
proc = subprocess.Popen(command, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
proc.stdin.write("dir c:\\")

9 个答案:

答案 0 :(得分:40)

简单地说:

import os
os.system('dir c:\\')

答案 1 :(得分:15)

你可能想尝试这样的事情:

command = "cmd.exe /C dir C:\\"

我认为你不能进入cmd.exe ...如果你是来自unix背景,那么,cmd.exe有一些难看的疣!

编辑:根据Sven Marnach的说法,你可以管道到cmd.exe。我在python shell中试过跟随:

>>> import subprocess
>>> proc = subprocess.Popen('cmd.exe', stdin = subprocess.PIPE, stdout = subprocess.PIPE)
>>> stdout, stderr = proc.communicate('dir c:\\')
>>> stdout
'Microsoft Windows [Version 6.1.7600]\r\nCopyright (c) 2009 Microsoft Corporatio
n.  All rights reserved.\r\n\r\nC:\\Python25>More? '

正如你所看到的,你还有一些工作要做(只返回第一行),但你可能能够让它工作......

答案 2 :(得分:9)

尝试:

import os

os.popen("Your command here")

答案 3 :(得分:6)

尝试在写入管道后添加对proc.stdin.flush()的调用,看看事情是否开始按预期运行。明确刷新管道意味着您无需担心如何设置缓冲区。

此外,不要忘记在命令末尾包含"\n",否则您的子shell将在提示符处等待完成命令输入。

我写了一篇关于使用Popen更详细地操作外部shell实例的文章:Running three commands in the same process with Python

就像那个问题中的情况一样,如果你需要在Windows机器上的多个进程外调用中维护shell状态,这个技巧就很有价值。

答案 4 :(得分:3)

同时使用'和'对我来说非常有用(Windows 10,python 3)

>>>import os
>>>os.system('"some cmd command here"')

例如,打开我的网络浏览器,我可以使用此:

>>>os.system('"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"')

答案 5 :(得分:2)

您为什么要拨打cmd.execmd.exe是命令行(shell)。如果要更改目录,请使用os.chdir("C:\\")。如果Python可以提供,请尽量不要调用外部命令。实际上,大多数操作系统命令都是通过os模块(和sys)提供的。我建议您查看os模块文档,了解各种可用方法。

答案 6 :(得分:1)

从达伦·托马斯(Daren Thomas)的答案中汲取灵感(并进行编辑),然后尝试以下操作:

proc = subprocess.Popen('dir C:\\', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = proc.communicate()

out现在将包含文本输出。

这里的关键之处在于subprocess module已经为您提供了与shell=True的shell集成,因此您不需要直接调用cmd.exe。

提醒一下,如果您使用的是Python 3,它将是字节,因此您可能需要进行out.decode()转换为字符串。

答案 7 :(得分:0)

在Python中,您可以直接使用下面的代码

import subprocess
proc = subprocess.check_output('C:\Windows\System32\cmd.exe /k %windir%\System32\\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f' ,stderr=subprocess.STDOUT,shell=True)
    print(str(proc))

在刚刚执行的用户帐户设置中的第一个参数中,您可以使用自己的参数进行自定义。

答案 8 :(得分:0)

很简单。你只需要两行代码,只需要使用内置函数,它就会接受输入并永远运行,直到你停止它。还有引号中的'cmd',保留它并且不要更改它。代码如下:

library(dplyr)

#Helper function to add tags
tagger = function(id, date, string){

#get time difference between consecutive dates
  time_diff = diff(date) %>% as.numeric()  
  tags = c()
  if (length(time_diff) !=0){
    for (i in 1:length(time_diff)){
      tag_temp = sapply(cumsum(time_diff[i:length(time_diff)]), FUN = function(x) (x>90) & (x<366))
      tags[i] = sum(tag_temp)
    }
  }
  
  return(data.frame(tag_ = as.numeric(  c(tags,0 )), date =date )) #return data_frame, add 0 for the last entry
}


df <- data.frame(id = c(1,1,1,1,1,2,2,2), date = as.Date(c("2020-01-01","2020-02-01","2020-03-01","2020-03-28","2020-08-15","2020-01-01","2020-05-01","2020-03-01")), string = c('a','b','b','a','a','a','a','b'))

#for each id and string, check for occurrence using tagger function
intermediat_result = df %>% group_by(id,string) %>% arrange(date) %>% do(tagger(.$id,.$date,.$string))

#return the final result
df %>% left_join(intermediat_result) 

#    id  date     string tag_
# 1  1 2020-01-01      a    1
# 2  1 2020-02-01      b    0
# 3  1 2020-03-01      b    0
# 4  1 2020-03-28      a    1
# 5  1 2020-08-15      a    0
# 6  2 2020-01-01      a    1
# 7  2 2020-05-01      a    0
# 8  2 2020-03-01      b    0

现在只需运行此代码,即可在您的 Python 项目中查看整个 Windows 命令提示符!

相关问题