Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > subprocess call acts differently than command line call?

Reply
Thread Tools

subprocess call acts differently than command line call?

 
 
Jim
Guest
Posts: n/a
 
      06-07-2007
Hello,

I need a program that will traverse a directory tree to ensure that
there
are unix-style line endings on every file in that tree that is a text
file.
To tell text files from others I want to use the unix "file" command
(Python's "mimetypes" is not so good for me). But I am stuck on
something about getting that output, and I'd greatly appreciate any
pointers.

Both the command line "file" and the python libmagic binding give the
same behavior, but I'll illustrate with "file". It sometimes acts
differently when run from the command line than when run using
the subprocess module (with the same user). For example, it
sometimes
gives output when run from the command line but no output when run as
a subprocess.

Below is a short program to demo. (I use this on a test file tree
that is at ftp://joshua.smcvt.edu/pub/hefferon/a.zip if anyone
is interested.)

.............................................
import subprocess
import glob

for fn in glob.glob('a*/*'):
cmd=['/usr/bin/file',fn]
cmdStr=" ".join(cmd)
try:

p=subprocess.Popen(cmdStr,shell=True,stdin=subproc ess.PIPE,stdout=subprocess.PIPE,stderr=subprocess. PIPE,close_fds=True)
(child_stdin,
child_stdout,
child_stderr)=(p.stdin,p.stdout,p.stderr)
stdoutContent=child_stdout.read()
except Exception, err:
mesg=u"unable to execute %s" % (repr(cmdStr),)
raise StandardError, mesg+": "+str(err)
if (p.returncode):
mesg=u"trouble executing %s" % (repr(cmdStr),)
raise StandardError, mesg+": "+repr(p.returncode)
print "result: ",stdoutContent
print "done"
.............................................

I've put a transcript of what happens at the bottom of this message.
One file (in the test tree it is "eqchange.txt") gives no output from
the above program, but does give an output when I use "file" at the
command line.

Specifying "-m/usr/share/file/magic" in the "file" call doesn't
change
that the command line and subprocess calls act differently, so it is
not just a question of different environments causing the system to
use different "magic" files. Changing the PIPE's to files, then
closing
and reopening them also does not matter, I believe.

In short I expected subprocess to just mimic my typing it in. Is
there some reason "file" doesn't act this way, and is there some
way to make it do so?

I have Python 2.4.4 running on Ubuntu. Thank you for any suggestions,
Jim

-----transcript (edited to shorten)------------
$ python test.py
result: acrotex/readme.txt: ASCII English text, with CRLF line
terminators

result: acrotex/eq2db.ins: ASCII English text, with CRLF line
terminators

result: acrotex/eqchange.txt:

result: acrotex/exerquiz.dtx: ISO-8859 English text, with CRLF line
terminators
result: acrotex/doc: directory

done

$ file acrotex/eqchange.txt
acrotex/eqchange.txt: ISO-8859 English text, with CRLF line
terminators
$ file acrotex/eqchange.txt 1> test.out
$ cat test.out
acrotex/eqchange.txt: ISO-8859 English text, with CRLF line
terminators
$ file acrotex/eqchange.txt 2> test.out
acrotex/eqchange.txt: ISO-8859 English text, with CRLF line
terminators
$ cat test.out
$

 
Reply With Quote
 
 
 
 
Rob Wolfe
Guest
Posts: n/a
 
      06-07-2007
Jim <(E-Mail Removed)> writes:

> Hello,
>
> I need a program that will traverse a directory tree to ensure that
> there
> are unix-style line endings on every file in that tree that is a text
> file.
> To tell text files from others I want to use the unix "file" command
> (Python's "mimetypes" is not so good for me). But I am stuck on
> something about getting that output, and I'd greatly appreciate any
> pointers.
>
> Both the command line "file" and the python libmagic binding give the
> same behavior, but I'll illustrate with "file". It sometimes acts
> differently when run from the command line than when run using
> the subprocess module (with the same user). For example, it
> sometimes
> gives output when run from the command line but no output when run as
> a subprocess.
>
> Below is a short program to demo. (I use this on a test file tree
> that is at ftp://joshua.smcvt.edu/pub/hefferon/a.zip if anyone
> is interested.)


[...]

> I've put a transcript of what happens at the bottom of this message.
> One file (in the test tree it is "eqchange.txt") gives no output from
> the above program, but does give an output when I use "file" at the
> command line.


[...]

> -----transcript (edited to shorten)------------
> $ python test.py
> result: acrotex/readme.txt: ASCII English text, with CRLF line
> terminators
>
> result: acrotex/eq2db.ins: ASCII English text, with CRLF line
> terminators
>
> result: acrotex/eqchange.txt:
>
> result: acrotex/exerquiz.dtx: ISO-8859 English text, with CRLF line
> terminators
> result: acrotex/doc: directory
>
> done
>
> $ file acrotex/eqchange.txt
> acrotex/eqchange.txt: ISO-8859 English text, with CRLF line


That's interesting. I've checked this eqchange.txt file from
your a.zip on my machine and the result is:

$ file acrotex/readme.txt
acrotex/readme.txt: ASCII English text, with CRLF line terminators
$ file acrotex/eqchange.txt
acrotex/eqchange.txt:
$ file acrotex/exerquiz.dtx
acrotex/exerquiz.dtx: ISO-8859 English text, with CRLF line terminators

$ file -v
file-4.17
magic file from /etc/magic:/usr/share/file/magic
$ uname -orvm
2.6.18-4-k7 #1 SMP Wed May 9 23:42:01 UTC 2007 i686 GNU/Linux

That's really strange. Have you got only *one* version of ``file``
program on your machine?

--
HTH,
Rob
 
Reply With Quote
 
 
 
 
Jim
Guest
Posts: n/a
 
      06-07-2007
On Jun 7, 2:30 pm, Rob Wolfe <(E-Mail Removed)> wrote:
> That's really strange. Have you got only *one* version of ``file``
> program on your machine?

Thank you, Rob. I thought I had that kind of thing covered in
specifying the -m but obviously there was something I wasn't getting,
and you found it.

Hope I can return the favor sometime,
Jim

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UpdatePanel acts differently inside Content area of Master Page John Kotuby ASP .Net 3 02-15-2008 01:10 AM
== operator acts differently in perl 5.005_03 and 5.8.2 Yahav Perl Misc 10 03-23-2005 03:07 PM
\Q acts differently in s/// and m// operations gargoyle Perl Misc 3 02-17-2005 10:57 PM
JS file handled differently than in-line script block bas jaburg ASP .Net 0 02-11-2005 08:25 AM
Web Form acts differently when using C# than VB Joe Fallon ASP .Net 1 02-15-2004 10:53 PM



Advertisments