All,

New to python programming so bare with me but I haven't quite seen an easy way to do this and looking for some suggestions.

I want to be able to replace some contents of an exisiting file either by both inserting new statements or modifying/replacing new ones withiout touching or modifying the other contents of the file. I will know ahead of time by whats in the property what I will be searching for. So It would be nice if the program would be flexible enough to figure out what to look for as well based on what new parameters are to be set from the property file.

Let me give a real-world example.

We have programs with start-scripts. For example. start.sh. (this is only an example and not actual so ignore the typos. Its only there to illustrate my question"

Inside start.sh there may be the following statements:

CLASSPATH=$CLASSPATH:$SOMEPATH
DB_DRIVER=example.jar:abc.jar:def.jar
PATH=$PATH1:$PATH2:$PATH3:/opt/data:/tmp:/apps/data/example1
./startup

What I want to be able to do is have a property file that says contains additional path and classpath variables that I want to append do it. In the future this property file could just have DB_DRIVER information or another value thats not listed in the example.

So I'll need to be able to read the file and recognize that I've reached the classpath statement as well as others and modify it appending my contents to it (either before or after) the existing values, while not losing them.

Like:
CLASSPATH=$NEWVALUEABCD:$CLASSPATHA:$SOMEPATH:NEWVALUE1:NEWVALUE2

Second I may want to be able to place a newline of statements anywhere in the file or after a certain line. For example, I may want to insert new lines into the file after DB_DRIVER or before ./startup and will pull those values out of this property file.

Any suggestions or code snippets is greatly appreciated.

Alan

A very useful project. :)

(1) You might want to check out the Python docs for the file and string objects. In particular, you could look at the following functions:

file.open
file.readline
file.readlines
string.replace
string.startswith

(2) You might want to build up to the final project in pieces: write a version of 'type' (or UNIX, 'cat'). Then write something that replaces a target line with an entirely new one. Then write something that modifies a line. Then write your final code.

Here's a simple snippet that opens a file and adds or appends a DB_DRIVER entry:

new_driver = "new.jar"
f = open("mytestfile.bat", "r")
lines = f.readlines()
f.close()
for line in lines:
    if line.startswith('DB_DRIVER'):
        i = lines.index(line)
        line = line.strip('\n')
        line += ':'+new_driver+'\n'
        lines[i] = line
        break
else:
       lines.append('DB_DRIVER=%s\n' % new_driver)
f = open("mytestfile.bat", "w")
f.writelines(lines)
f.close()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.