473,395 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Total No. of "Records" in a File?

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/>
Aug 23 '08 #1
8 1844
-----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.

W. eWatson wrote:
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?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkiwSZ4ACgkQLMI5fndAv9hXugCeJs5XBkLLne 6ljqQggB/MoAVs
SNIAoJxsU04cwcZMrH9QjElAbMD34RdK
=RlmP
-----END PGP SIGNATURE-----
Aug 23 '08 #2
On 2008-08-23, W. eWatson <no*******@sbcglobal.netwrote:
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?
If the lines are fixed lengh (e.g. always 12 bytes long), then
you can use os.stat() or os.fstat() to find the size of the
file. Divide the size of the file by the number of bytes in a
line, and you get the number of lines.

--
Grant
Aug 23 '08 #3
W. eWatson wrote:
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>

Aug 23 '08 #4
Nick Dumas wrote:
-----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.

W. eWatson wrote:
>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?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkiwSZ4ACgkQLMI5fndAv9hXugCeJs5XBkLLne 6ljqQggB/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/>
Aug 23 '08 #5
On 23.08.2008, W. eWatson <no*******@sbcglobal.netwroted:
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
--
Grzegorz Staniak <gstaniak _at_ wp [dot] pl>
Aug 23 '08 #6
Fredrik Lundh wrote:
W. eWatson wrote:
>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>
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/>
Aug 23 '08 #7
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 ?
Aug 26 '08 #8
W. eWatson a écrit :
Fredrik Lundh wrote:
>W. eWatson wrote:
>>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>
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.
Aug 26 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Duke Hamacher | last post by:
Hello: This title is a little ambiguous but this is an interesting puzzle. Lets's say we have a JSP page with a form. The ActionForm associated with this form has, say, 4 String properities. ...
0
by: J. Muenchbourg | last post by:
I'm using and If with the BOF and EOF methods to start looping thru a group of records: If rstime.BOF and rstime.EOF Then response.write "There are no records!" <%Else%> <%If Not rstime.BOF...
3
by: Vic | last post by:
Dear All, I have a database of laboratory records in Access 2000. There is one form which acts as an interface to input experimetal data. This form incorporates information from several tables....
1
by: Leslie | last post by:
I need to get 20 random records from a table based on a condition. The condition isn't a problem. I have tried select top 20 *, rnd(field) from table, but that gets me the top 20 records. I need...
2
by: Marlon Brown | last post by:
Hello, I am able to "edit" and "update" records using Gridview and Data Sources. However I don't see an option to "insert" records. Can someone tell me what's the procedure to "insert"...
1
by: scottmachado | last post by:
I am currently running a marco that run a query and emails the results as an attachment in excel format. If the query has no records, I would like to email "no records found" in the first cell in...
0
by: sherifffruitfly | last post by:
Hi, I'm deleting a bunch of records from my DB, and they actually DO get deleted (as I verify by looking afterwards). But then I get the exception in the subject line. Isn't it weird that all of...
7
by: bbasberg | last post by:
Hello, I thought I would do a "sanity check" by asking the experts how to do the following: I need to process external data (it will be in an Access DB table and will be updated daily) into a...
1
by: psyvanz | last post by:
any code that can delete all records in just one click if you have a dataenvironment code it more good.. cause im working in this kind of code. like this: single delete code (not all in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.