Script to read and open multiple files

This is the place for queries that don't fit in any of the other categories.

Script to read and open multiple files

Postby doc11 » Tue Jun 11, 2013 1:02 pm

Hello,

Anyone can help me out..

I want to create code to modify some files this way.

In a file called 'servers.db' will be a list of 200 server hostnames e.g.

california
texas
....
....

Then there is a folder containing the configuration file for each of those server in .cfg format.

First thing is open servers.db and get the hostname.
Second then open the .cfg file using that hostname, in a folder path e.g. c:\temp\california.cfg
Third, write a new file called "hostname".conf
Fourth, paste a few lines in the "hostname".conf e.g. this is the "hostname" server blah blah.
Fifth, repeat the above for every "hostname" entry in servers.db list i.e then do texas, then atlanta, then kansas etc..

I am stuck at getting hostname to be automatically picked by the script.
doc11
 
Posts: 5
Joined: Tue Jun 11, 2013 12:44 pm

Re: Script to read and open multiple files

Postby hansn » Tue Jun 11, 2013 2:48 pm

doc11 wrote:I am stuck at getting hostname to be automatically picked by the script.


I'm guessing the file 'servers.db' is in fact just a regular text file (?)

If the file is written like you said, with one hostname on each line, you can simply read the file one line at a time, extracting one hostname at a time.

Here you can look at some basic stuff about reading text files in python: http://docs.python.org/2/tutorial/input ... ting-files
See the readline() method for how to read one line at a time.
hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: Script to read and open multiple files

Postby doc11 » Tue Jun 11, 2013 3:24 pm

The pseudocode I created is about 4000 lines long as I wanted a template for successful result that I can compare when I shorten the code. So I know the basic stuff.


All I want is create one module with a "general parameter". This parameter will change with the "hostname" and needs to be an input that came as a result from reading a line in the .db file.


inp = open ("c:/temp/hostname.cfg")
opf = open ("c:/temp/conf/hostname.conf")
for line in inp:
opf.write ("hostname+line \n)
opf.write ("Wait for string "hostname" \n)

How can I get the hostname to be picked from a file called server.txt for example. Rather than point me to a "basic stuff" url to read you can give pointers of a method I can employ.
doc11
 
Posts: 5
Joined: Tue Jun 11, 2013 12:44 pm

Re: Script to read and open multiple files

Postby hansn » Tue Jun 11, 2013 4:40 pm

4000 lines of pseudocode? wow

doc11 wrote:How can I get the hostname to be picked from a file called server.txt for example. Rather than point me to a "basic stuff" url to read you can give pointers of a method I can employ.

Sorry but I don't understand what you need help with.
Why don't you just go through every server in the server.txt file, open the corresponding cfg file and then write to the new conf file?
hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: Script to read and open multiple files

Postby doc11 » Tue Jun 11, 2013 5:07 pm

Sorry I don't know how much easier I can describe it...who knows.. engineerng realm doesn't exactly offer subtle descriptions. That is exactly what I have been doing and it works fine but then you end up with massive amount of code. 20 lines x 200 servers = 4000 lines of code.
For every new server I add, I have to create a new set of 20 lines that look exactly the same as the previous server, with the difference being just the "hostname" entries.

So it is better to create a template of 20 lines. Then run this same template against every new server I add. Then all i need to modify is the servers.txt file to add the new hostname/server.


The whole scenario is a little bit complicated so I am only telling you the bit I need help with. It is probably quite simple in python because I could do it for vbs no sweat. I am using vbs to call this py script just to perform that bit.
doc11
 
Posts: 5
Joined: Tue Jun 11, 2013 12:44 pm

Re: Script to read and open multiple files

Postby hansn » Tue Jun 11, 2013 7:36 pm

Oh. I don't know how much python you know, this sounds like it can easily be implemented with a function.
If you could show me the code you have , or at least one of the 20-liners, I could probably help you out much easier. (always include some code when you can when asking for help)

Whenever you find yourself repeating a lot of code, you should put it in a function:
Code: Select all
def write_conf(hostname):
    f = open('c:/temp/%s.cfg' % hostname)
    conf = open('c:/temp/conf/%s.conf' % hostname, 'w')
    while 1:
        some_server_info = f.readline()
        if not server:
            break
        conf.write(some_server_info)

This will of course be used by a script (or another function) that iterates over all the hostnames in the server.txt file :)
Code: Select all
f = open('server.db')
while 1:
    hostname = f.readline()
    if not hostname:
        break
    write_conf(hostname)
f.close()


Does this look like what you want?
hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: Script to read and open multiple files

Postby micseydel » Wed Jun 12, 2013 2:39 am

hansn wrote:
Code: Select all
f = open('server.db')
while 1:
    hostname = f.readline()
    if not hostname:
        break
    write_conf(hostname)
f.close()

Typically this code would be written more like
Code: Select all
with open('server.db') as f:
    for hostname in f:
        write_conf(hostname)

(they are not entirely equivalent, the latter version does some exception handling for free on top of everything else you can see it does)
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1131
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Script to read and open multiple files

Postby doc11 » Wed Jun 12, 2013 10:16 am

Spot on, that's what I am looking for. Nice one hansn & micseydel.

I get a traceback though as he is also adding "\n" after the host name. How to prevent that?

Code: Select all
Traceback (most recent call last):
  File "<pyshell#111>", line 3, in <module>
    write_conf(hostname)
  File "<pyshell#109>", line 2, in write_conf
    f = open("c:/temp/%s.cfg' % hostname" )
OSError: [Errno 22] Invalid argument: 'c:/temp/california\n.cfg'
doc11
 
Posts: 5
Joined: Tue Jun 11, 2013 12:44 pm

Re: Script to read and open multiple files

Postby micseydel » Wed Jun 12, 2013 10:45 am

Replace
Code: Select all
hostname

with
Code: Select all
hostname.strip()
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1131
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Script to read and open multiple files

Postby doc11 » Wed Jun 12, 2013 12:24 pm

Thank you very much.

Worked perfectly.
doc11
 
Posts: 5
Joined: Tue Jun 11, 2013 12:44 pm


Return to General Coding Help

Who is online

Users browsing this forum: gutschy and 4 guests