Printing a "status " line from a python script

C

Chris Seymour

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
 
S

Steve Holden

Chris said:
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
 
C

Chris Rebert

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..... 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
 
A

Alex VanderWoude

Chris said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top