[Tutor] Different Command Result Executing in Shell vs. Program

Alan Gauld alan.gauld at btinternet.com
Sun May 10 02:36:21 CEST 2009


"robert mcquirt" <pjsjoca at yahoo.com> wrote

> import os
> os.system('file -b /home/robert/linuxlogotag')
>   
> This also works fine. The problem is that when I try to replace 
> the hard coded path/name with a string variable for looping, 
> the results are not the same as the shell's. 

> import os, glob
> path = '/home/robert'
> for infile in glob.glob( os.path.join(path, '*.*') ):
>     testCommand = "'file -b " + infile + "'"
>     print testCommand,
>     test = os.system(testCommand)

The problem is that the return code from os.system is the 
exit code of the command being executed - usually 0 for 
success or an error code. What you want is to actually 
capture the output of the command. To do that you need 
to look at the subprocess module which provides several 
examples  of what you want.
   
> output is not the same as shell results. All files, regardless 
> of type, simply output the number 32512, with no description.

That is the exit code of the file -b command. You would 
need to look at the file documentation to find out what 
it signifies!
   
PS. You can also find some simple examples using 
subprocess in the Using the OS topic of my tutorial.

HTH,

Alan G.



More information about the Tutor mailing list