<tfoot id='5spT6'></tfoot>
    <legend id='5spT6'><style id='5spT6'><dir id='5spT6'><q id='5spT6'></q></dir></style></legend>
    1. <small id='5spT6'></small><noframes id='5spT6'>

      • <bdo id='5spT6'></bdo><ul id='5spT6'></ul>

      <i id='5spT6'><tr id='5spT6'><dt id='5spT6'><q id='5spT6'><span id='5spT6'><b id='5spT6'><form id='5spT6'><ins id='5spT6'></ins><ul id='5spT6'></ul><sub id='5spT6'></sub></form><legend id='5spT6'></legend><bdo id='5spT6'><pre id='5spT6'><center id='5spT6'></center></pre></bdo></b><th id='5spT6'></th></span></q></dt></tr></i><div id='5spT6'><tfoot id='5spT6'></tfoot><dl id='5spT6'><fieldset id='5spT6'></fieldset></dl></div>

    2. 在子进程中使用 * 通配符的命令问题

      时间:2023-07-21

        <i id='FAmfl'><tr id='FAmfl'><dt id='FAmfl'><q id='FAmfl'><span id='FAmfl'><b id='FAmfl'><form id='FAmfl'><ins id='FAmfl'></ins><ul id='FAmfl'></ul><sub id='FAmfl'></sub></form><legend id='FAmfl'></legend><bdo id='FAmfl'><pre id='FAmfl'><center id='FAmfl'></center></pre></bdo></b><th id='FAmfl'></th></span></q></dt></tr></i><div id='FAmfl'><tfoot id='FAmfl'></tfoot><dl id='FAmfl'><fieldset id='FAmfl'></fieldset></dl></div>
            <tbody id='FAmfl'></tbody>
          <legend id='FAmfl'><style id='FAmfl'><dir id='FAmfl'><q id='FAmfl'></q></dir></style></legend>
          • <tfoot id='FAmfl'></tfoot>
          • <small id='FAmfl'></small><noframes id='FAmfl'>

                <bdo id='FAmfl'></bdo><ul id='FAmfl'></ul>
                本文介绍了在子进程中使用 * 通配符的命令问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试使用子进程库和 Popen 方法将文件从一个位置复制到另一个位置.运行以下脚本时,我收到错误 cp: cannot stat/some/dev_path/*.有人告诉我 * 没有扩展到文件名,这就是问题所在.同样在其他一些帖子中,人们建议使用 call 而不是 Popen,但据我所知 call 不会返回 stderr.

                I'm trying to copy files from one location to another using subprocess library and Popen method. When runing following script I'm getting the error cp: cannot stat /some/dev_path/*. I was told that the * is not expanded to the file names and that's where the problem is. Also in some other posts people were suggesting to use call instead of Popen, but call will not return stderr as far as I know.

                devPath = '/some/dev_path/'
                productionPath = '/some/prod_path/'
                
                p = subprocess.Popen(['cp', '-r', devPath + '*', productionPath], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
                pout, perr = p.communicate()
                
                if perr != '':
                    sys.exit('Error: ' + perr)
                

                推荐答案

                扩展 * (globbing) 是你的 shell 的一个函数,例如 bash.因此,您必须在 subprocess.Popen 调用中使用关键字参数 shell=True.

                Expanding the * (globbing) is a function of your shell, bash for example. Therefore you'd have to use the keyword argument shell=True in your subprocess.Popen call.

                但是,对于这种情况,我强烈建议改用 shutil.copytree.

                However, for this case I'd strongly suggest to use shutil.copytree instead.

                (首先,因为它更简单(参见 Python 之禅) 并且不易出错.处理错误更加简洁,您会得到很好的异常,包括错误列表(对于像您这样的多文件操作),并且您不必处理生成子进程和通信其次,如果您不需要,分叉子进程是不必要的资源浪费.其他问题包括引用/转义,如果您未能正确清理用户输入,可能会在您的代码中引入安全漏洞.)

                (First of all, because it's much simpler (see Zen of Python) and less error-prone. Dealing with errors is much cleaner, you get nice exceptions including a list of errors (for multi file operations like yours), and you don't have to deal with spawning a subprocess and communicating with it. Second, it's an unnecessary waste of resources to fork a child process if you don't need to. Other issues include quoting / escaping and possibly introducing security vulnerabilities into your code if you fail to properly sanitize user input.)

                例如:

                from shutil import copytree
                from shutil import Error
                
                try:
                   copytree('dir_a', 'dir_b')
                except (Error, OSError), e:
                    print "Attempt to copy failed: %s" % e
                

                此外,您不应通过将字符串连接在一起来构建文件系统路径,而应使用 os.path.join().这将为当前操作系统使用正确的目录分隔符 (os.sep),并允许您轻松编写可移植代码.

                Also, you shouldn't build filesystem paths by concatenating strings together, but instead use os.path.join(). That will use the correct directory separator (os.sep) for the current OS and allow you to easily write portable code.

                例子:

                >>> import os
                >>> os.path.join('/usr/lib', 'python2.7')
                '/usr/lib/python2.7'
                

                注意:os.path.join 仍然只进行(智能)字符串操作 - 它不关心该路径是否可访问甚至是否存在.

                Note: os.path.join still only does (smart) string manipulation - it doesn't care if that path is accessible or even exists.

                这篇关于在子进程中使用 * 通配符的命令问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Python ClearCase 下载 Vobs Popen 密码 BASH 程序 Sketchy 下一篇:ping 无限时间并在 Python 中获取其输出

                相关文章

                <tfoot id='WvPiM'></tfoot>
              • <i id='WvPiM'><tr id='WvPiM'><dt id='WvPiM'><q id='WvPiM'><span id='WvPiM'><b id='WvPiM'><form id='WvPiM'><ins id='WvPiM'></ins><ul id='WvPiM'></ul><sub id='WvPiM'></sub></form><legend id='WvPiM'></legend><bdo id='WvPiM'><pre id='WvPiM'><center id='WvPiM'></center></pre></bdo></b><th id='WvPiM'></th></span></q></dt></tr></i><div id='WvPiM'><tfoot id='WvPiM'></tfoot><dl id='WvPiM'><fieldset id='WvPiM'></fieldset></dl></div>
                1. <legend id='WvPiM'><style id='WvPiM'><dir id='WvPiM'><q id='WvPiM'></q></dir></style></legend>

                      <bdo id='WvPiM'></bdo><ul id='WvPiM'></ul>

                    <small id='WvPiM'></small><noframes id='WvPiM'>