Can't write to a directory made w/ os.makedirs

Dave Angel d at davea.name
Mon Jan 2 09:09:27 EST 2012


On 01/01/2012 10:14 PM, David Goldsmith wrote:
> On Jan 1, 7:05 am, Tim Golden<m... at timgolden.me.uk>  wrote:
>> On 01/01/2012 12:05, David Goldsmith wrote:
>>   >>  ie can the Python process creating the directories,
>>   >
>>   >  Yes.
>>   >
>>   >>  and a subprocess called from it create a simple file?
>>   >
>>   >  No.
>>   >
>>   >>  Depending on where you are in the filesystem, it may indeed
>>   >>  be necessary to be running as administrator. But don't try
>>   >>  to crack every security nut with an elevated sledgehammer.
>>   >
>>   >  If you mean running as admin., those were my sentiments exactly.  So,
>>   >  there isn't something specific I should be doing to assure that my
>>   >  subproceses can write to directories?
>>
>> In the general case, no. By default, a subprocess will have
>> the same security context as its parent. The exception is
>> where the parent (the Python processing invoking subprocess.call
>> in this example) is already impersonating a different user;
>> in that case, the subprocess will inherit its grandparent's
>> context.
>>
>> But unless you're doing something very deliberate here then
>> I doubt if that's biting you.
>>
>> Can I ask: are you absolutely certain that the processes
>> you're calling are doing what you think they are and failing
>> where you think they're failing?
>>
>> TJG
> I'm a mathematician: the only thing I'm absolutely certain of is
> nothing.
>
> Here's my script, in case that helps:
>
> import os
> import sys
> import stat
> import os.path as op
> import subprocess as sub
> from os import remove
> from os import listdir as ls
> from os import makedirs as mkdir
>
> def doFlac2Mp3(arg, d, fl):
>      if '.flac' in [f[-5:] for f in fl]:
>          newD = d.replace('FLACS', 'MP3s')
>          mkdir(newD)
>          for f in fl:
>              if f[-5:]=='.flac':
>                  root = f.replace('.flac', '')
>                  cmd = ['"C:\\Program Files (x86)\\aTunes\\win_tools\
> \flac.exe" -d ' +
>                         '--output-prefix=' + newD + '\\', f]
>                  res = sub.call(cmd)#, env={'PATH': os.defpath})
>                  if not res:
>                      cmd = ['"C:\\Program Files (x86)\\aTunes\\win_tools
> \\lame.exe" -h',
>                              newD + root + '.wav',  newD + root +
> '.mp3']
>                      res = sub.call(cmd)#, env={'PATH': os.defpath})
>                      if not res:
>                          rf = newD + root + '.wav'
>                          remove(rf)
>
> top=sys.argv[1]
> op.walk(top, doFlac2Mp3, None)
The line cmd= is bogus.  You're trying to run a program with a -h after 
the filename.  The reason you're passing a list to sub.call is to 
separate the parameters from the program name, not to mention with 
quotes in its name.  So the -h has to be  a separate list item.  
(Although Windows will probably handle it correctly if you combine the 
-h with the FOLLOWING argument, it's still bad practice]

cmd = [ "c:\\program files .... \\lame.exe", "-h", newD + root + ....."



-- 

DaveA




More information about the Python-list mailing list