在循环中创建txt文件时出错

时间:2017-03-25 18:22:54

标签: python

逐行写入文件的所有IP地址,它将由给定的命令行执行。我想将结果保存到每个文件中,文件的名称代表每个IP。

with open(list) as l:
    for line in l:
            file = line+".txt"
            os.system("whois "+line+" >> "+file)

列表包含:

192.168.0.1
192.168.0.2
192.168.0.3

错误:

sh: 3: .txt: not found
sh: 3: .txt: not found
sh: 3: .txt: not found

1 个答案:

答案 0 :(得分:3)

您应该在for循环开始时添加line = line.strip()以清除\n变量中的line,这将解决它。

请参阅此answer以获取有关python的调试方法的示例。在您的情况下,print()语句可以帮助您查看正在生成的内容。