본문 바로가기
파이썬

파이썬 Python에서 subprocess.call ( 'dir', shell = True)를 사용할 때 지정된 파일을 찾을 수 없습니다.

by º기록 2020. 12. 30.
반응형

32 비트 Python 2.7이 설치된 64 비트 시스템에서 다음을 수행하려고합니다.

import subprocess
p = subprocess.call('dir', shell=True)
print p

그러나 이것은 나에게 준다.

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    p = subprocess.call('dir', shell=True)
  File "C:\Python27\lib\subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "C:\Python27\lib\subprocess.py", line 709, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 957, in _execute_child
    startupinfo)
  WindowsError: [Error 2] The system cannot find the file specified

내가 터미널에 있으면 ...

dir

... 물론 현재 폴더 내용을 인쇄합니다.

쉘 매개 변수를 shell = False로 변경하려고했습니다.

편집 : 실제로 subprocess.call () 을 사용하여 경로에서 실행 파일을 호출 할 수 없습니다. p = subprocess.call ( 'dir', shell = True) 문은 다른 컴퓨터에서 잘 작동하며 관련이 있다고 생각합니다.

만약 내가한다면

 subprocess.call('PATH', shell=True)

다음 나는 얻는다

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    subprocess.call('PATH', shell=True)
  File "C:\Python27\lib\subprocess.py", line 522, in call
     return Popen(*popenargs, **kwargs).wait()
  File "C:\Python27\lib\subprocess.py", line 709, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 957, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

만약 내가한다면:

import os
print os.curdir

다음 나는 얻는다

.

위의 모든 작업은 관리자 모드로 시작된 터미널에서 실행됩니다.

 

해결 방법

 

COMSPEC 환경 변수에 문제가있을 수 있습니다.

>>> import os
>>> os.environ['COMSPEC']
'C:\\Windows\\system32\\cmd.exe'
>>> import subprocess
>>> subprocess.call('dir', shell=True)

    (normal output here)

>>> os.environ['COMSPEC'] = 'C:\\nonexistent.exe'
>>> subprocess.call('dir', shell=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Python27\lib\subprocess.py", line 493, in call
    return Popen(*popenargs, **kwargs).wait()
  File "c:\Python27\lib\subprocess.py", line 679, in __init__
    errread, errwrite)
  File "c:\Python27\lib\subprocess.py", line 896, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

이 잠재적 인 문제는 subprocess.py 를 파헤 치고, 역 추적에서 지적한 _execute_child 함수를 살펴봄으로써 발견되었습니다. 여기서 if shell : 로 시작하는 블록을 찾을 수 있습니다.이 블록은 해당 변수에 대한 환경을 검색하고이를 사용하여 프로세스를 시작하는 데 사용되는 인수를 생성합니다.

 

참조 페이지 https://stackoverflow.com/questions/20330385

 

 

반응형

댓글