Manipulate file in Windows

D-Man dsh8290 at rit.edu
Tue Jun 5 16:03:51 EDT 2001


On Thu, May 31, 2001 at 08:53:01PM +0000, Dublanc, David wrote:
| Hello,
| I have to manipulate big text files (more than 100 M ) on Windows and Unix.
| 
| I have several questions :
| 
| How can I count lines fastly ? ( wc -l in Unix...)

$ time man bash | wc -l
    5544

real    0m0.081s
user    0m0.030s
sys     0m0.040s

$ time man bash | python -c "import sys ; print sys.stdin.read().count( '\n' ) "
5544

real    0m0.221s
user    0m0.100s
sys     0m0.030s


Quite a bit slower.  Perhaps check the implementation of wc for more speed?

| When I have the total number of lines, I have to split the file into x files
| of 1000 lines. How can I do this fastly ?

Why do you need to count the number of lines in the first place?
Couldn't you just start stuffing groups of 1000 lines into other files
and count the lines as you go?

| Are there functions to replace x lines of a file by y another lines ? To
| delete the x to y lines in a file ?
| 
| In brief, I am looking for useful file functions that I cannot find in the
| Library reference....

These functions aren't really possible on files themselves, but rather make
more sense on a string or list of strings (the data in the file).  A
file is just a series of bytes on a disk.  Each block has a certain
location.  Random modification only works if the new data is the exact
same size as the old data.

-D





More information about the Python-list mailing list