簡體   English   中英

帶有python子進程運行的AttributeError

[英]AttributeError with python subprocess run

Windows 7 64位上的Python 3.7.1 64位

test.py

import subprocess
subprocess.run(['ping localhost'], shell=True)

產量

C:\>python test.py
Traceback (most recent call last):
  File "test.py", line 2, in <module>
    subprocess.run(['ping localhost'], shell=True, check=True)
  File "C:\Program Files\Python37\lib\site-packages\run\__init__.py", line 145, in __new__
    process = cls.create_process(command, stdin, cwd=cwd, env=env, shell=shell)
  File "C:\Program Files\Python37\lib\site-packages\run\__init__.py", line 121, in create_process
    shlex.split(command),
  File "C:\Program Files\Python37\lib\shlex.py", line 305, in split
    return list(lex)
  File "C:\Program Files\Python37\lib\shlex.py", line 295, in __next__
    token = self.get_token()
  File "C:\Program Files\Python37\lib\shlex.py", line 105, in get_token
    raw = self.read_token()
  File "C:\Program Files\Python37\lib\shlex.py", line 136, in read_token
    nextchar = self.instream.read(1)
AttributeError: 'list' object has no attribute 'read'

為什么會出現此錯誤,該如何解決?

這不是特定於ping命令,而是任何瑣碎的命令,例如“ dir”

經過反復的嘗試和拔頭發后,我開始工作了。

應該改為

subprocess.run('ping localhost', shell=True) #Remove the square brackets

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM