<bdo id='DkyGX'></bdo><ul id='DkyGX'></ul>
  1. <small id='DkyGX'></small><noframes id='DkyGX'>

    <tfoot id='DkyGX'></tfoot>

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

    1. <legend id='DkyGX'><style id='DkyGX'><dir id='DkyGX'><q id='DkyGX'></q></dir></style></legend>

      subprocess.Popen 使用相对路径

      时间:2023-07-21

          <legend id='col9Z'><style id='col9Z'><dir id='col9Z'><q id='col9Z'></q></dir></style></legend>
            <tbody id='col9Z'></tbody>

          <tfoot id='col9Z'></tfoot>

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

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

                本文介绍了subprocess.Popen 使用相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                Popen 的 docs 提到你可以't 指定相对于更改工作目录"kwarg 的可执行路径.

                The docs for Popen mention that you can't specify your executable path relative to the 'change working directory' kwarg.

                如果cwd不为None,则孩子的当前目录将变为cwd 在执行之前.注意这个目录不是搜索可执行文件时考虑,因此您不能指定程序相对于 cwd 的路径.

                If cwd is not None, the child’s current directory will be changed to cwd before it is executed. Note that this directory is not considered when searching the executable, so you can’t specify the program’s path relative to cwd.

                但是 python 在我的系统上的行为似乎直接与这个说法相矛盾:

                But python's behaviour on my system seems to directly contradict this claim:

                /tmp$ mkdir a
                /tmp$ cp /bin/ls /tmp/a/my_ls
                /tmp$ mkdir b
                /tmp$ touch /tmp/b/potato
                /tmp$ cd /home/wim
                ~$ python
                Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
                [GCC 4.8.1] on linux2
                Type "help", "copyright", "credits" or "license" for more information.
                >>> from subprocess import check_output
                >>> check_output(['../a/my_ls'], cwd='/tmp/b')
                'potato
                '
                >>> check_output(['../a/my_ls'])
                OSError: [Errno 2] No such file or directory
                

                使用 cwd 的相对路径是否依赖于平台且不应依赖?或者这是一个文档错误?

                Is using relative paths to cwd something that is platform dependent and shouldn't be relied upon? Or is this a documentation bug?

                (这个问题来自 glglgl 这里的评论)

                (this question spawns from a comment by glglgl here)

                推荐答案

                是的,这取决于平台.

                在 POSIX 系统上,进程是分叉的,在子进程中,在执行可执行文件之前会执行 os.chdir(cwd).

                On POSIX systems, the process is forked and in the child process a os.chdir(cwd) is executed before executing the executable.

                然而,在 Windows 上,CreateProcess() API 调用 被使用,cwd 作为 lpCurrentDirectory 参数传入.不会发生目录更改,并且 CreateProcess() 调用在查找要执行的 lpApplicationName 时不会 参考该参数.

                On Windows however, the CreateProcess() API call is used and cwd is passed in as the lpCurrentDirectory parameter. No directory change takes place, and the CreateProcess() call does not consult that parameter when looking for the lpApplicationName to execute.

                为了让您的应用程序跨平台,您在查找可执行文件时不应依赖于更改当前工作目录.

                To keep your application cross-platform, you should not rely on the current working directory to be changed when looking up the executable.

                这篇关于subprocess.Popen 使用相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:带有引号和变量的 Python 子进程命令 下一篇:Python 'subprocess' CalledProcessError: Command '

                相关文章

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

                <legend id='msQqE'><style id='msQqE'><dir id='msQqE'><q id='msQqE'></q></dir></style></legend>

                  1. <tfoot id='msQqE'></tfoot>