Reputation: 19
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!
Upvotes: 1
Views: 357
Reputation: 71535
I see a couple of errors in your code.
os.popen('mkdir -p /Astronomy/' + DATE + '/' TIME) # this line works for DATE only
This is a syntax error. I think you meant to have '/' + TIME
, not '/' TIME
. I'm not sure what you mean by "this line works for DATE only"?
subprocess.call(['mkdir', '-p', '/Astronomy/', DATE]), #THIS LINE DOESN'T WORK
What command do you expect to call? I'm guessing from the rest of your code that you're trying to execute mkdir -p /Astronomy/<<DATE>>
. That isn't what you've coded though. Each item in the list you pass to subprocess.call
is a separate argument, so what you've written comes out as mkdir -p /Astronomy <<DATE>>
. This will attempt to create two directories, a root-level directory /Astronomy
, and another one in the current working directory named whatever DATE
is.
If I'm correct about what you wanted to do, the corrected line would be:
subprocess.call(['mkdir', '-p', '/Astronomy/' + DATE])
chown's answer using os.makedirs
(and using os.path.join
to splice paths, rather than string +) is a better general approach, in my opinion. But this is why your current code isn't working, as far as I can tell.
Upvotes: 1
Reputation: 52778
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
for any cp
/mv
/etc operations.
From the os
Docs:
os.makedirs(path[, mode])
Recursive directory creation function. Likemkdir()
, 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.
Upvotes: 2