【问题标题】:Using the subprocess or Command module of python for Windows command prompt在 Windows 命令提示符下使用 python 的子进程或命令模块
【发布时间】:2013-07-30 01:09:36
【问题描述】:

我正在学习 python 并正在观看谷歌课程以开始学习。我对第三个作业有一些问题。我们应该从目录中找到特殊文件,然后通过在命令提示行中输入命令来复制或压缩它们。

复制文件的函数是这样的:

def copy(dir,todir):    
    filenames=os.listdir(dir)

    for file in filenames:
       cmd='copy'+' '+file+' '+todir 
       print(cmd)
       (status,output)=subprocess.getstatusoutput(cmd) 
         print(output)
    return

错误表明

'{' not recognized as an internal or external command,operable program or batch file.

subprocess.getstatusoutput 方法在将它发送到 os.popen() 之前,在 cmd 中添加一个 '{' 和其他一些东西。

我希望看到“复制的 x 个文件”作为输出。

我使用的是 Windows 8。

你知道问题是什么吗?会不会是操作系统的原因?关于如何修复它的任何想法?

视频使用 python2.7,我使用的是 python3.2,其中命令模块被子进程模块替换。两者的getstatusoutput方法一样吗?

【问题讨论】:

    标签: python command-line subprocess


    【解决方案1】:

    Documentation 说: cmd 实际上是这样运行的

    { cmd ; } 2>&1
    

    getstatusoutput() 函数在 UNIX 上可用,在 Windows 上可用。

    【讨论】:

    • 尝试以下操作:process = subprocess.Popen( cmd + params, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=False) 并获得输出out = process.stdout.read()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 2019-01-26
    • 2012-11-15
    • 2013-06-17
    • 1970-01-01
    • 2016-10-12
    相关资源
    最近更新 更多