How to use multiple variables read from file with looping subprocess/popen
Question: I am using python to read 2 files from my linux os. One contains a single
entry/number 'DATE':

>
>     20111125
>  

the other file contains many entries, 'TIME':

>
>     042844UTC
>     044601UTC
>     ...
>     044601UTC
>  

I am able to read the files to assign to proper variables. I would like to
then use the variables to create folder paths, move files etc... such as:

>
>     $PATH/20111125/042844UTC
>     $PATH/20111125/044601UTC
>     $PATH/20111125/044601UTC
>  

and so on.

Somehow this doesn't work with multiple variables passed at once:

    
    
    import subprocess, sys, os, os.path
    DATEFILE = open('/Astronomy/Sorted/2-Scratch/MAPninox-DATE.txt', "r")
    TIMEFILE = open('/Astronomy/Sorted/2-Scratch/MAPninox-TIME.txt', "r")
    for DATE in DATEFILE:
        print DATE,
    for TIME in TIMEFILE:
        os.popen('mkdir -p /Astronomy/' + DATE + '/' TIME) # this line works for DATE only
        os.popen('mkdir -p /Astronomy/20111126/' + TIME) # this line works for TIME only
        subprocess.call(['mkdir', '-p', '/Astronomy/', DATE]), #THIS LINE DOESN'T WORK
    

Thanks!


Answer: I would suggest using `os.makedirs` (which does the same thing as `mkdir -p`)
instead of `subprocess` or `popen`:

    
    
    import sys
    import os
    DATEFILE = open(os.path.join(r'/Astronomy', 'Sorted', '2-Scratch', 'MAPninox-DATE.txt'), "r")
    TIMEFILE = open(os.path.join(r'/Astronomy', 'Sorted', '2-Scratch', 'MAPninox-TIME.txt'), "r")
    
    for DATE in DATEFILE:
        print DATE,
    
    for TIME in TIMEFILE:
        os.makedirs(os.path.join(r'/Astronomy', DATE, TIME))
    
        astrDir = os.path.join(r'/Astronomy', '20111126', TIME)
        try
            os.makedirs(astrDir)
        except os.error:
            print "Dir %s already exists, moving on..." % astrDir
        # etc...
    

Then use [`shutil`](http://docs.python.org/library/shutil.html) for any
`cp`/`mv`/etc operations.

* * *

From the [`os` Docs](http://docs.python.org/library/os.html):

> **`os.makedirs(path[, mode])`**  
>  Recursive directory creation function. Like `mkdir()`, but makes all
> intermediate-level directories needed to contain the leaf directory. Raises
> an error exception if the leaf directory already exists or cannot be
> created. The default mode is 0777 (octal). On some systems, mode is ignored.
> Where it is used, the current umask value is first masked out.

