views:

450

answers:

3

I am looking to create a python script that will read one source file then produce another file with a string for the name.

for example

macaddress.cnf.xml contains the source file

I need to change '6000' to '6001' in multiple places of macaddress.cnf.xml, then I want to output to newmacaddress.cnf.xl.

This is what I have

#! /usr/bin/python
#read and write to file
f = open(file)
for line in f:
    if line.contains('66001'):
        newline = line.replace('66001', '60001')

To add I would like to be able to do this with a csv or whatever and have the script run through and do

60002 >> macaddressfromcsv.cnf.xml 60003 >> macaddressfromcsv.cnf.xml 60004 etc.

Sorry I am very new to this any help would be great.

+1  A: 

It is unclear from your question what all of your goals are, but I will try to address some of them. If you want to take input from one file, modify it, and then write to another file you could do following:

buffer = "";
with open("input_file") as in:
    buffer = in.read();

# do modifications ...

with open("output_file", 'w') as out:
    out.write(buffer);

Note: you would need to use from __future__ import with_statement in python version 2.5.

To do multiple replacements you can actually use re.sub() function:

buffer = re.sub('6000', '6001', buffer)

As to the the additional information using csv, please provide a more detailed explanation or an example of what you are trying to achieve.

artdanil
regex is not needed for his case.
A: 
data=open("input_file").read()
outfile=open("macaddressfromcsv.cnf.xml","w")
for i in range(6001,6010):
    outfile.write(data.replace("6000",str(i))
    outfile.write("\n") # you may not need this
outfile.close()

Edit. I think you need to clarify the question. Is there only one output file or to you want one file per replacement?

gnibbler
A: 

Sorry, I am new to this all,

End of the day i need to do this

input a large number of macaddress via csv or whatever

Have the script take these addresses one by one

create a new file with the mac in the name

fill the file with same contents as the original but find and replace a number string in order

ie. create file macaddress2 ,find and replace all '60001' with '60002'

then for the next macaddress create macaddress3 and find and replace '60001' with '60003'

I need this to be able to run off a *nix or windows machine to a solaris box

If I am missing anything let me know.. thanks for all the help

slack31337