【问题标题】:Error to create the txt file in loop循环创建txt文件时出错
【发布时间】:2017-03-25 18:22:54
【问题描述】:

所有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

【问题讨论】:

    标签: python


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢。我的错,这很容易,现在我才想通
    • 没问题,我们都去过!欢迎加入社区。​​span>
    猜你喜欢
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    相关资源
    最近更新 更多