473,395 Members | 1,442 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,395 software developers and data experts.

Simple question for all of you python gurus

I am writing a little script to grep through a really long list of files and
look for a certain phrase.

I wanted to do something like this.

li = os.popen("/usr/bin/grep -i %s /home/%s/.tmda/pending" %(phrase,userid)
lis = li.readlines()
blah blah blah...

but even if I run this from the command line I get an error saying the
argument list is too long.

However, if I cd to /home/userid/.tmda/pending and then run the grep from
within the directory... it works great.

so I tried to do something like this
os.system("cd /home/%s/.tmda/pending" %userid)
li = os.popen("/usr/bin/grep -i %s" %phase)
lis = lis.readlines()

But since cd is a shell command, it does not seem to work either.

How can I do this?
Thanks in advance for answering this seemingly easy question.

Jul 18 '05 #1
5 1416
> How can I do this?
Thanks in advance for answering this seemingly easy question.


You're code might work if you use os.chdir to change the current working
directory.

Another that works on the command-line (and thus for your script) is to use
xargs with -l<num>, e.g.:

find / | xargs -l 10 grep -i foo

xargs then will invoke grep in batches of ten.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
In article <NYu%b.12366$qL1.145@fed1read02>,
Sean Berry <se********@cox.net> wrote:
Jul 18 '05 #3
Thanks to you both.

os.chdir ... I knew it was something simple. Sorry for the wasted
bandwidth.

Cameron Laird <cl****@lairds.com> wrote in message
news:10*************@corp.supernews.com...
In article <NYu%b.12366$qL1.145@fed1read02>,
Sean Berry <se********@cox.net> wrote:
.
.
.
so I tried to do something like this
os.system("cd /home/%s/.tmda/pending" %userid)
li = os.popen("/usr/bin/grep -i %s" %phase)
lis = lis.readlines()

But since cd is a shell command, it does not seem to work either.

How can I do this?

.
.
.
import os

os.chdir("/home/%s/.tmda/pending" % userid)
li = os.popen(...
--

Cameron Laird <cl****@phaseit.net>
Business: http://www.Phaseit.net

Jul 18 '05 #4
On Thu, 26 Feb 2004 23:14:33 +0000, Cameron Laird wrote:
os.chdir("/home/%s/.tmda/pending" % userid)
li = os.popen(...


Wouldn't something like this also work?

li = os.popen('/bin/sh -c "cd /home/%s; /usr/bin/grep -i %s blah"')

This has the advantage that the current directory of the Python program
doesn't change.

Jeremy

Jul 18 '05 #5
In article <pa****************************@jeremysanders.net> ,
Jeremy Sanders <je*************@jeremysanders.net> wrote:
On Thu, 26 Feb 2004 23:14:33 +0000, Cameron Laird wrote:
os.chdir("/home/%s/.tmda/pending" % userid)
li = os.popen(...


Wouldn't something like this also work?

li = os.popen('/bin/sh -c "cd /home/%s; /usr/bin/grep -i %s blah"')

This has the advantage that the current directory of the Python program
doesn't change.

Jul 18 '05 #6

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

Similar topics

28
by: Erik Johnson | last post by:
This is somewhat a NEWBIE question... My company maintains a small RDBS driven website. We currently generate HTML using PHP. I've hacked a bit in Python, and generally think it is a rather...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
16
by: sb | last post by:
Hello I have a bunch of questions about using python scripting in vim and about editing python with vim. I can install vim with python scripting support, but I can not find docs, examples...
37
by: michele.simionato | last post by:
Paul Rubin wrote: > How about macros? Some pretty horrible things have been done in C > programs with the C preprocessor. But there's a movememnt afloat to > add hygienic macros to Python. Got any...
5
by: Xah Lee | last post by:
© # the following is a example of defining © # a function in Python. © © def fib(n): © """This prints n terms of a sequence © where each term is the sum of previous two, © starting...
31
by: surfunbear | last post by:
I've read some posts on Perl versus Python and studied a bit of my Python book. I'm a software engineer, familiar with C++ objected oriented development, but have been using Perl because it is...
137
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very...
25
by: abhinav | last post by:
Hello guys, I am a novice in python.I have to implement a full fledged mail server ..But i am not able to choose the language.Should i go for C(socket API) or python for this project? What are the...
267
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.