Total No. of "Records" in a File?

W

W. eWatson

I have an ordinary text file with a CR at the end of a line, and two numbers
in each line. Is there some way to determine the number of lines (records)
in the file before I begin reading it?

--
Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: <www.speckledwithstars.net/>
 
F

Fredrik Lundh

W. eWatson said:
I have an ordinary text file with a CR at the end of a line, and two
numbers in each line. Is there some way to determine the number of lines
(records) in the file before I begin reading it?

In the general case, no. A file is just a bunch of bytes. If you know
that all lines have exactly the same length, you can of course fetch the
file size and divide by the line size, but that doesn't work for
arbitrary files.

Why do you need to know the number of lines before reading it, btw?

</F>
 
W

W. eWatson

Nick said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Err...you want to know what is in a file before you open it? This could
be done if you keep some external database documenting changes made to
the file. But unless I misunderstand what you're saying, then it's not
possible to know the contents of a file without opening and reading that
file.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkiwSZ4ACgkQLMI5fndAv9hXugCeJs5XBkLLne6ljqQggB/MoAVs
SNIAoJxsU04cwcZMrH9QjElAbMD34RdK
=RlmP
-----END PGP SIGNATURE-----
Maybe. I could see it if the file were truly in a record format. The # of
records might be kept by the OS. It's conceivable that Python or the OS
might see a file with a CR as "recordized". All unlikely though. Just checkin'.

How about in a slightly different case. Suppose I want to know the number of
files in a folder? The OS and maybe some Python method might know that.

--
Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: <www.speckledwithstars.net/>
 
G

Grzegorz Staniak

Maybe. I could see it if the file were truly in a record format. The # of
records might be kept by the OS. It's conceivable that Python or the OS
might see a file with a CR as "recordized".

Isn't it much easier to use a database instead? That's what they're made for.
How about in a slightly different case. Suppose I want to know the number of
files in a folder? The OS and maybe some Python method might know that.

Use "os" and "os.path". For a simple case the length of "os.listdir()" could
suffice, but then you might need to filter out sub-directories, or maybe
count files in them too using "os.walk()".

GS
 
W

W. eWatson

Fredrik said:
In the general case, no. A file is just a bunch of bytes. If you know
that all lines have exactly the same length, you can of course fetch the
file size and divide by the line size, but that doesn't work for
arbitrary files.

Why do you need to know the number of lines before reading it, btw?

</F>
Actually, it was a matter of curiosity, and maybe absent mindedness. I was
envisioning a program where I might want to run up and down a file a lot,
sometimes deleting a record interactively at the request of the user.
However, I wanted to keep him alert to the total number of records
remaining. However, in retrospect, I more likely do this with files in a
folder. I also want him to be able to skip around in the Win OS folder by
saying something like go forward 3 files. I'd like not to have to read all
the files between the two points. The whole idea needs some more thinking.

--
Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: <www.speckledwithstars.net/>
 
B

Bruno Desthuilliers

W. eWatson a écrit :
I have an ordinary text file with a CR at the end of a line, and two
numbers in each line. Is there some way to determine the number of lines
(records) in the file before I begin reading it?

How could you know how many times a given character appears in file
without reading the whole file ?

You could of course store metadata about one file in another file[1],
but then you'd have to read and parse this other file, and it might go
out of sync.

[1] or at the begining of your 'data' file - but you still have to rad
at least this part, and you still have the potential sync problem.

Or you could use a fixed-size binary format for your records, and try
dividing the file size by the record size.

What's your concrete use case, exactly ?
 
B

Bruno Desthuilliers

W. eWatson a écrit :
Actually, it was a matter of curiosity, and maybe absent mindedness. I
was envisioning a program where I might want to run up and down a file a
lot, sometimes deleting a record interactively at the request of the
user. However, I wanted to keep him alert to the total number of records
remaining. However, in retrospect, I more likely do this with files in a
folder. I also want him to be able to skip around in the Win OS folder
by saying something like go forward 3 files. I'd like not to have to
read all the files between the two points. The whole idea needs some
more thinking.
The whole idea is that you should learn what a DBMS is good for, IMHO.
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top