迭代文件中的行

时间:2018-03-21 06:49:48

标签: python python-2.7

我正在尝试从文件中读取每一行,每行内容唯一的IP,在读取每个IP后,我正在为每个IP运行shell脚本,但似乎这只发生在最后一行,即文件末尾的最后一个IP。 / p>

with open("/root/nix_bsd_mac_inventory-master/Unique.txt") as fp:
        for line in fp:
                print "Processing Ip: " + line

p = subprocess.Popen(["./d42_linux_autodisc_v620"], stdout=subprocess.PIPE)

print p.communicate()

我想让它为每个ip运行直到最后一行(ip)结束

我的Unique.txt看起来像:

12.xx.xx.16
12.xx.xx.17
12.xx.xx.18

我得到的输出:

Processing Ip: 12.xx.xx.16
Processing Ip: 12.xx.xx.17
Processing Ip: 12.xx.xx.18

Done in 100 seconds\n\nProcessed 1 IPs\n1 devices were successfully added/updated\n', None) -- ran for only last ip/line.

我希望它为每个IP运行,直到最后一行(IP)结束。

1 个答案:

答案 0 :(得分:0)

您有缩进问题。在subprocess.Popen声明中执行for loop

例如:

with open("/root/nix_bsd_mac_inventory-master/Unique.txt") as fp:
        for line in fp:
            print "Processing Ip: " + line
            p = subprocess.Popen(["./d42_linux_autodisc_v620 ", line], stdout=subprocess.PIPE)    
            print p.communicate()