473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

subprocess call acts differently than command line call?

Jim
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 1test.out
$ cat test.out
acrotex/eqchange.txt: ISO-8859 English text, with CRLF line
terminators
$ file acrotex/eqchange.txt 2test.out
acrotex/eqchange.txt: ISO-8859 English text, with CRLF line
terminators
$ cat test.out
$

Jun 7 '07 #1
2 3355
Jim <ji**********@gmail.comwrites:
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
Jun 7 '07 #2
Jim
On Jun 7, 2:30 pm, Rob Wolfe <r...@smsnet.plwrote:
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

Jun 7 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stewart Midwinter | last post by:
this has me puzzled; I've created a small test app to show the problem I'm having. I want to use subprocess to execute system commands from inside a Tkinter app running under Cygwin. When I...
0
by: Christoph Haas | last post by:
Evening, I'm having trouble with running a process through Python 2.4's subprocess module. Example code: ======================================================== def run(command): run =...
0
by: Corey Wallis | last post by:
Dear All, I'm currently working on a project that needs to collect the output of the JHOVE application. More information about the application is available at this website: ...
1
by: Jim | last post by:
Hello, I'm trying to use subprocess to drive a Perl script. I'm having some trouble getting it to spot the command line arguments. Basically, if I call subprocess(args).wait() where args has a...
12
by: Eric_Dexter | last post by:
I am trying to modify a programming example and I am coming up with two problems... first is that I can't seem to pass along the arguments to the external command (I have been able to do that with...
9
by: Phoe6 | last post by:
Hi all, Consider this scenario, where in I need to use subprocess to execute a command like 'ping 127.0.0.1' which will have a continuous non- terminating output in Linux. # code # This...
12
by: bhunter | last post by:
Hi, I've used subprocess with 2.4 several times to execute a process, wait for it to finish, and then look at its output. Now I want to spawn the process separately, later check to see if it's...
7
by: skunkwerk | last post by:
Hi, i'm trying to call subprocess.popen on the 'rename' function in linux. When I run the command from the shell, like so: rename -vn 's/\.htm$/\.html/' *.htm it works fine... however when I...
4
by: Tobiah | last post by:
I am not sure how to capture the output of a command using subprocess without creating a temp file. I was trying this: import StringIO import subprocess file = StringIO.StringIO() ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.