Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python for subprocess.Popen()和os.system()上的循环我得到TypeError:';int';对象是不可编辑的_Python_Python 3.x_Subprocess_Os.system - Fatal编程技术网

python for subprocess.Popen()和os.system()上的循环我得到TypeError:';int';对象是不可编辑的

python for subprocess.Popen()和os.system()上的循环我得到TypeError:';int';对象是不可编辑的,python,python-3.x,subprocess,os.system,Python,Python 3.x,Subprocess,Os.system,假设我已经编写了以下代码: import os import subprocess for i in os.system('ls'): print i 我有以下错误: Traceback (most recent call last): File "<console>", line 1, in <module> TypeError: 'int' object is not iterable 我也有同样的问题。如果我只做os.system('ls')或

假设我已经编写了以下代码:

import os
import subprocess

 for i in os.system('ls'):
    print i
我有以下错误:

 Traceback (most recent call last):
 File "<console>", line 1, in <module>
 TypeError: 'int' object is not iterable
我也有同样的问题。如果我只做os.system('ls')或subprocess.Popen('ls',shell=True)。wait()

在输出之后出现的额外“0”将导致问题。有什么办法摆脱它吗?

如果你想摆脱它:

proc = subprocess.Popen("ls", shell=True, stdout=subprocess.PIPE)
for line in proc.stdout:
    print(line)
os.system()
(如果成功,则为0)

由于明显的原因,您不能对整数进行迭代,因此必须获取可重用的数据

您需要获取作为输出的字符串,对其进行解析,然后对其进行迭代。
函数应该是您的工作方式。

0
是流程的返回代码。这意味着它运行时没有问题。您希望
system
返回哪些可以迭代的内容?调用
ls
的结果被输出到
sys.stdout
。没关系,如何消除错误TypeError:“int”对象不是iterableIf您想列出目录,在python中执行会返回一个您可以迭代的列表。不要尝试使用进程返回代码进行迭代谢谢我一直在寻找的。
  db.sqlite3  ip  manage.py  mysite  
  0
proc = subprocess.Popen("ls", shell=True, stdout=subprocess.PIPE)
for line in proc.stdout:
    print(line)