473,698 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.speckledwi thstars.net/>
Aug 23 '08 #1
8 1850
-----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

iEYEARECAAYFAki wSZ4ACgkQLMI5fn dAv9hXugCeJs5XB kLLne6ljqQggB/MoAVs
SNIAoJxsU04cwcZ MrH9QjElAbMD34R dK
=RlmP
-----END PGP SIGNATURE-----
Aug 23 '08 #2
On 2008-08-23, W. eWatson <no*******@sbcg lobal.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

iEYEARECAAYFAki wSZ4ACgkQLMI5fn dAv9hXugCeJs5XB kLLne6ljqQggB/MoAVs
SNIAoJxsU04cwcZ MrH9QjElAbMD34R dK
=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.speckledwi thstars.net/>
Aug 23 '08 #5
On 23.08.2008, W. eWatson <no*******@sbcg lobal.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.speckledwi thstars.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
2176
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. Very simple. Remember, we only have one form. Now let's say each the 4 String properties together comprise a record and the user is able to view and edit a variable number of records at
0
4833
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 Then%> <!-- the old Here are your results header --> <% Do While Not rstime.EOF
3
2575
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. I have a flag (yes/no field) indicating whether a particular experiment (one record) is completed (ie all data belonging to that record is inputted and quality controlled). What I want is that when I change the flag to "yes" the particular record...
1
1433
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 20 random records. I would prefer to use SQL only for this if it is possible. Thanks in advance.
2
1914
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" records from ASP.NET - SQL db using Gridview ?
1
2716
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 the excel attachment. I have a feeling that this needs to be done in VB, but I don't know that much about VB. Would using a report instead be a better and easier way of doing this?
0
1345
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 the correct records would get deleted, and only *after* that, an error gets thrown? Here's the code I'm using - thanks for any ideas! try
7
1871
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 local Access DB table that has the same structure with additional fields. The local table is prexisting and the designer used an autonumber field as its key. There is a "real key" between the two tables (let's call it PK1 for this discussion (it's...
1
2274
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 records) dataenvironment1.command1.delete thats all i can do... i want to know how to create a code that can delete all records code in just a single click... of a command button..
0
8680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9030
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.