代码之家  ›  专栏  ›  技术社区  ›  digitaLink

子进程没有此类文件或目录错误。Python中的调用

  •  1
  • digitaLink  · 技术社区  · 7 年前

    Python脚本:

    from subprocess import call
    call(['read', '-ep', 'Path:', 'temporaryPath'])
    print temporaryPath
    

    错误回溯:

    Traceback (most recent call last):
      File "tmp.py", line 2, in <module>
        call(['read', '-ep', 'Path:', 'temporaryPath'])
      File "/usr/lib64/python2.6/subprocess.py", line 478, in call
        p = Popen(*popenargs, **kwargs)
      File "/usr/lib64/python2.6/subprocess.py", line 642, in __init__
        errread, errwrite)
      File "/usr/lib64/python2.6/subprocess.py", line 1238, in _execute_child
        raise child_exception
    OSError: [Errno 2] No such file or directory
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   Julien Palard    7 年前

    你想打电话 read 这是一个外壳内置:

    $ type read
    read is a shell builtin
    

    这个特定的shell内置没有等效的程序:

    $ which read
    $ 
    

    PATH 环境变量,根据 strace :

    [pid 17266] execve("/usr/local/bin/read", ["read", "-ep", "Path:", "temporaryPath"], [/* 70 vars */]) = -1 ENOENT (No such file or directory)
    [pid 17266] execve("/usr/bin/read", ["read", "-ep", "Path:", "temporaryPath"], [/* 70 vars */]) = -1 ENOENT (No such file or directory)
    [pid 17266] execve("/bin/read", ["read", "-ep", "Path:", "temporaryPath"], [/* 70 vars */]) = -1 ENOENT (No such file or directory)
    [pid 17266] execve("/usr/local/games/read", ["read", "-ep", "Path:", "temporaryPath"], [/* 70 vars */]) = -1 ENOENT (No such file or directory)
    [pid 17266] execve("/usr/games/read", ["read", "-ep", "Path:", "temporaryPath"], [/* 70 vars */]) = -1 ENOENT (No such file or directory)
    […]
    [pid 17266] write(4, "OSError:", 8 <unfinished ...>
    

    阅读

    $ python3
    Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
    [GCC 6.3.0 20170118] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import subprocess
    >>> subprocess.call('read', shell=True)
    /bin/sh: 1: read: arg count
    2
    >>> subprocess.call('read foo', shell=True)
    hello world
    0
    

    您现在遇到了一个新问题:shell内置 阅读 正在将读取的值存储为shell变量,该变量将在调用 subprocess.call .

    哦,在 shell builtin你也没有完成。你应该用 input argparse