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

robert mcquirt pjsjoca at yahoo.com
Sun May 10 01:42:29 CEST 2009


  
  
  
  
  
  
   
  
 
  
   Hi. I've not been working with Python very long and have run into a puzzling thing. I'm working on a program that needs to identify the filetype of files without extensions. Using the file command in the shell works fantastic, as in:
   
   
robert at ubuntu:~$ file -b linuxlogotag
   JPEG image data, JFIF standard 1.01
   
   
I want to be able to perform the same task in a program for batch processing. After a bit of research, I found that I can execute the shell command from within a Python program using:
   
   
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. Here's the modified code I'm using for testing (the original code is from Bogdan's blog at http://bogdan.org.ua/).
   
   
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)
     print test
   
   
The code does indeed step through all the files in the directory and the testCommand string constructs properly. Yet, the output is not the same as shell results. All files, regardless of type, simply output the number 32512, with no description.

   
   I have searched forums, read the man file and perused several Python references and have not been able to find the answer. Why does the same command yield different results, depending on how it's run? Any education is greatly appreciated.

   
   Linux 2.6 (kubuntu 8.04)
   Python 2.5.2
   file command 4.21
  


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090509/1f24c059/attachment-0001.htm>


More information about the Tutor mailing list