Connecting Tech Pros Worldwide Forums | Help | Site Map

Printing a "status " line from a python script

Chris Seymour
Guest
 
Posts: n/a
#1: Nov 11 '08
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



Steve Holden
Guest
 
Posts: n/a
#2: Nov 11 '08

re: Printing a "status " line from a python script


Chris Seymour wrote:
Quote:
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/

Chris Rebert
Guest
 
Posts: n/a
#3: Nov 11 '08

re: Printing a "status " line from a python script


On Tue, Nov 11, 2008 at 11:02 AM, Chris Seymour <cgseymour@gmail.comwrote:
Quote:
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.
Quote:
Quote:
Quote:
>>for i in range(10):
.... print '\r'+str(i),
9
Quote:
Quote:
Quote:
>>>
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
Chris Seymour
Guest
 
Posts: n/a
#4: Nov 11 '08

re: Printing a "status " line from a python script


Thanks guys for the ideas.

Appreciate the help.

Cheers.

Chris
Alex VanderWoude
Guest
 
Posts: n/a
#5: Nov 13 '08

re: Printing a "status " line from a python script


Chris Seymour wrote:
Quote:
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
Closed Thread