Calling configuration file ID into Linux Command with Date Time from Python
Question: I'm trying to write a script to get the following outputs to a folder
(YYYYMMDDHHMMSS = current date and time) using a Linux command in Python, with
the ID's in a configutation file

    
    
    1234_YYYYMMDDHHMMSS.txt
    12345_YYYYMMDDHHMMSS.txt
    12346_YYYYMMDDHHMMSS.txt
    

I have a config file with the list of ID's

    
    
    id1 = 1234
    id2 = 12345
    id3 = 123456
    

I want to be able to loop through these in python and incorporate them into a
linux command.

Currently, my linux commands are hardcoded in python as such

    
    
    import subprocess
    import datetime
    
    now = datetime.datetime.now()
    subprocess.call('autorep -J 1234* -q > /home/test/output/1234.txt', shell=True)
    subprocess.call('autorep -J 12345* -q > /home/test/output/12345.txt', shell=True)
    subprocess.call('autorep -J 123456* -q > /home/test/output/123456.txt', shell=True)
    
    
    print now.strftime("%Y%m%d%H%M%S")
    

The datetime is defined, but doesn't do anything currently, except print it to
the console, when I want to incorporate it into the output txt file. However,
I want to be able to write a loop to do something like this

    
    
    subprocess.call('autorep -J id1* -q > /home/test/output/123456._now.strftime("%Y%m%d%H%M%S").txt', shell=True)
    subprocess.call('autorep -J id2* -q > /home/test/output/123456._now.strftime("%Y%m%d%H%M%S").txt', shell=True)
    subprocess.call('autorep -J id3* -q > /home/test/output/123456._now.strftime("%Y%m%d%H%M%S").txt', shell=True)
    

I know that I need to use ConfigParser and currently have been this piece
written which simply prints the ID's from the configuration file to the
console.

    
    
    from ConfigParser import SafeConfigParser
    import os
    
    
    parser = SafeConfigParser()
    parser.read("/home/test/input/ReportConfig.txt")
    
    def getSystemID():
        for section_name in parser.sections():
            print
            for key, value in parser.items(section_name):
                print '%s = %s' % (key,value)
        print
    
    
    getSystemID()
    

But as mentioned in the beggining of the post, my goal is to be able to loop
through the ID's, and incorporate them into my linux command while adding the
datetime format to the end of the file. I'm thinking all I need is some kind
of while loop in the above function in order to get the type of output I want.
However, I'm not sure how to call the ID's and the datetime into a linux
command.


Answer: So far you have most of what you need, you are just missing a few things.

First, I think using ConfigParser is overkill for this. But it's simple enough
so lets continue with it. Lets change `getSystemID` to a generator returning
your IDs instead of printing them out, its just a one line change.

    
    
    parser = SafeConfigParser()
    parser.read('mycfg.txt')
    def getSystemID():
        for section_name in parser.sections():
            for key, value in parser.items(section_name):
                yield key, value
    

With a generator we can use `getSystemID` in a loop directly, now we need to
pass this on to the subprocess call.

    
    
    # This is the string of the current time, what we add to the filename
    now = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
    
    # Notice we can iterate over ids / idnumbers directly
    for name, number in getSystemID():
        print name, number
    

Now we need to build the subprocess call. The bulk of your problem above was
knowing how to format strings, the syntax is described
[here](http://docs.python.org/2/library/string.html#formatstrings).

I'm also going to make two notes on how you use `subprocess.call`. First, pass
a list of arguments instead of a long string. This helps python know what
arguments to quote so you don't have to worry about it. You can read about it
in the [subprocess](http://docs.python.org/2/library/subprocess.html) and
[shlex](http://docs.python.org/2/library/shlex.html?highlight=shlex#shlex)
documentation.

Second, you redirect the output using `>` in the command and (as you noticed)
need `shell=True` for this to work. Python can redirect for you, and you
should use it.

To pick up where I left off above in the foor loop.

    
    
    for name, number in getSystemID():
        # Make the filename to write to
        outfile = '/home/test/output/{0}_{1}.txt'.format(number, now)
    
        # open the file for writing
        with open(outfile, 'w') as f:
            # notice the arguments are in a list
            # stdout=f redirects output to the file f named outfile
            subprocess.call(['autorep', '-J', name + '*', '-q'], stdout=f)
    

