0

我有以下脚本(见下文),它采用标准输入并处理成一些简单的文件。

# Import Modules for script
import os, sys, fileinput, platform, subprocess

# Global variables
hostsFile = "hosts.txt"
hostsLookFile = "hosts.csv"

# Determine platform
plat = platform.system()

if plat == "Windows":

# Define Variables based on Windows and process
    currentDir = os.getcwd()
    hostsFileLoc = currentDir + "\\" + hostsFile
    hostsLookFileLoc = currentDir + "\\" + hostsLookFile
    ipAddress = sys.argv[1]
    hostName = sys.argv[2]
    hostPlatform = sys.argv[3]
    hostModel = sys.argv[4]

    # Add ipAddress to the hosts file for python to process 
    with open(hostsFileLoc,"a") as hostsFilePython:
        for line in open(hostsFilePython, "r"):
            if ipAddress in line:
                print "ipAddress \(" + ipAddress + "\) already present in hosts file"
            else:
                print "Adding ipAddress: " + ipAddress + " to file"
                hostsFilePython.write(ipAddress + "\n")

    # Add all details to the lookup file for displaying on-screen and added value
    with open(hostsLookFileLoc,"a") as hostsLookFileCSV:
        for line in open(hostsLookFileCSV, "r"):
            if ipAddress in line:
                print "ipAddress \(" + ipAddress + "\) already present in lookup file"
            else:
                print "Adding details: " + ipAddress + "," + hostName + "," + hostPlatform + "," + hostModel + " to file"
                hostsLookFileCSV.write(ipAddress + "," + hostName + "," + hostPlatform + "," + hostModel + "\n")

if plat == "Linux":

# Define Variables based on Linux and process
    currentDir = os.getcwd()
    hostsFileLoc = currentDir + "/" + hostsFile
    hostsLookFileLoc = currentDir + "/" + hostsLookFile
    ipAddress = sys.argv[1]
    hostName = sys.argv[2]
    hostPlatform = sys.argv[3]
    hostModel = sys.argv[4]

    # Add ipAddress to the hosts file for python to process 
    with open(hostsFileLoc,"a") as hostsFilePython:
        print "Adding ipAddress: " + ipAddress + " to file"
        hostsFilePython.write(ipAddress + "\n")

    # Add all details to the lookup file for displaying on-screen and added value
    with open(hostsLookFileLoc,"a") as hostsLookFileCSV:
        print "Adding details: " + ipAddress + "," + hostName + "," + hostPlatform + "," + hostModel + " to file"
        hostsLookFileCSV.write(ipAddress + "," + hostName + "," + hostPlatform + "," + hostModel + "\n")

这段代码显然不起作用,因为for line in open(hostsFilePython, "r"):语法错误......我不能使用带有“ open()”的当前文件对象。但是,这是我想要实现的,我该怎么做?

4

2 回答 2

2

您想使用a+模式打开文件,以便您可以读取和写入,然后只需使用现有的文件对象 ( hostsFilePython)。

但是,这仍然不起作用,因为您只能在文件用尽之前对其进行一次迭代。

值得注意的是,这不是很有效。更好的计划是将数据读入一个集合,用新值更新集合,然后将集合写入文件。(正如评论中指出的那样,集合不保留重复项(对您的目的有益)和顺序,这可能对您有用也可能不适用。如果没有,那么您可能需要使用一个列表,这会降低效率)。

于 2012-06-14T12:40:25.750 回答
0
with open(hostsFileLoc) as hostsFilePython:
    lines = hostsFilePython.readlines()

for filename in lines:
    with open(hostsFileLoc, 'a') as hostFilePython:
        with open(filename) as hostsFile:
            for line in hostsFile.readlines():
                if ipAddress in line:
                   print "ipAddress \(" + ipAddress + "\) already present in hosts file"
                else:
                   print "Adding ipAddress: " + ipAddress + " to file"
                   hostsFilePython.write(ipAddress + "\n")

默认模式是读取,所以不需要r显式传入。

于 2012-06-14T12:44:18.727 回答