By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,592 Members | 1,473 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,592 IT Pros & Developers. It's quick & easy.

Printing a "status " line from a python script

P: n/a
Hi All,
I am working on a python script for my colleague that will walk a
directory and search in the different files for a specific string.
These pieces I am able to do. What my colleague wants is that when
she runs the script the filename is replaced by the current file that
is being processed. So instead of seeing a series of files being
listed down the page, she only wants to see the file currently being
processed.

I am not sure if this is even possible. Any thoughts would be greatly
appreciated.

Thanks.

Chris
Nov 11 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Chris Seymour wrote:
Hi All,
I am working on a python script for my colleague that will walk a
directory and search in the different files for a specific string.
These pieces I am able to do. What my colleague wants is that when
she runs the script the filename is replaced by the current file that
is being processed. So instead of seeing a series of files being
listed down the page, she only wants to see the file currently being
processed.

I am not sure if this is even possible. Any thoughts would be greatly
appreciated.
Try printing each filename with

sys.write("\r%s\r%s" % (" "*80, filename)

It's crappy, but it might just work (you may need to change the 80 to
some other numeric value).

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Nov 11 '08 #2

P: n/a
On Tue, Nov 11, 2008 at 11:02 AM, Chris Seymour <cg*******@gmail.comwrote:
Hi All,
I am working on a python script for my colleague that will walk a
directory and search in the different files for a specific string.
These pieces I am able to do. What my colleague wants is that when
she runs the script the filename is replaced by the current file that
is being processed. So instead of seeing a series of files being
listed down the page, she only wants to see the file currently being
processed.

I am not sure if this is even possible. Any thoughts would be greatly
appreciated.
Have you tried using carriage returns ("\r")?

chris ~ $ python
Python 2.5.1 (r251:54863, Feb 4 2008, 21:48:13)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>for i in range(10):
.... print '\r'+str(i),
9
>>>
Note how each line gets overwritten by the next so that we only see
the final number output (9).

But really, I don't see a good reason to do this. So what, the output
takes up some extra lines on the terminal? Big whoop. Your colleague
can either pipe the script to `less` or a file if it really bothers
them. And that way you get a list of all files processed, which can
often come in handy in my experience.

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
>
Thanks.

Chris
--
http://mail.python.org/mailman/listinfo/python-list
Nov 11 '08 #3

P: n/a
Thanks guys for the ideas.

Appreciate the help.

Cheers.

Chris
Nov 11 '08 #4

P: n/a
Chris Seymour wrote:
I am working on a python script for my colleague that will walk a
directory and search in the different files for a specific string.
These pieces I am able to do. What my colleague wants is that when
she runs the script the filename is replaced by the current file that
is being processed. So instead of seeing a series of files being
listed down the page, she only wants to see the file currently being
processed.
You should know that printing "\b" issues a backspace. You can couple
this with printing without line endings using a trailing comma:

print "Now processing file", # Trailing comma = no line ending
previous = ""
names = ["one", "two", "three"]
for name in names:
if previous:
print "\b" * (len(previous) + 2), # Trailing comma again
print name, # Yet another trailing comma
previous = name
# Do whatever other processing you need here
print # At last a line ending

Hope this helps.
- Alex
Nov 13 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.