Newbie Needs help!!

B

Bryano

Hi,

I am new to the world of Python and am currently taking my first steps and
have already hit a brick wall, here is my example code:

-------------------------------------------------------------------
#!/usr/bin/python
import sys
import os
import string
import math

InFile = file("testfile","r") 'In my code I have the full file
path to the file
FileCon = InFile.read()
FileLines = InFile.readline()
print FileCon
print FileLines
InFile.close()

--------------------------------------------------------------------

My testfile contains 2 lines of text i.e

I am Line 1
I am Line 2

What I expect this to show when run is the following:

I am Line 1
I am Line 2
2

However it omits the number of lines in this case 2 ( which i get using the
line FileLines = InFile.readline() )
it would appear that by referencing InFile once that it clears out its
contents so null is always returned when
calling the readline(). If I omit the line that starts FileCon (and
associated print line) it prints the file length correctly as 2.

Any ideas as to why this is happening?? its driving me crazy at the
moment...

Any help appreciated.

Cheers

Rigga
 
S

Scott David Daniels

Bryano wrote:

....
FileCon = InFile.read()
At this point you have read the entire file.
FileLines = InFile.readline()
So there is nothing to read here.
-- Oh, I see, you expect readline() to return the line number.
It returns the next line in the file (which is empty).

You need to count the lines yourself, if you want a count.

-Scott David Daniels
(e-mail address removed)
 
R

Rigga

Scott said:
Bryano wrote:

...
At this point you have read the entire file.
So there is nothing to read here.
-- Oh, I see, you expect readline() to return the line number.
It returns the next line in the file (which is empty).

You need to count the lines yourself, if you want a count.

-Scott David Daniels
(e-mail address removed)

Thanks for your reply, I was under the misunderstanding that I could read
the contents of the file and get the total number of lines the file
contains in one go. Any pointers to how I can count the lines manually??

Regards
 
M

Michael Geary

Rigga said:
Thanks for your reply, I was under the misunderstanding that I
could read the contents of the file and get the total number of
lines the file contains in one go. Any pointers to how I can count
the lines manually??

You actually can do the whole thing in one line of code:

print len( file('testfile').readlines() )

I think that leaves the file open until the file object gets garbage
collected, though, so it's better to explicitly close the file as you did in
your original code.

Also, readlines() reads the entire file into a list, which of course will
use enough memory to hold the entire file. Of course, depending on what else
you're doing with the file, it may be handy to have all of the file lines in
a list. But if you just want to get the line count with minimal memory
overhead, you can write your own loop:

def countLines( filename ):
lines = 0
f = file( filename )
while f.readline():
lines += 1
f.close()
return lines

print countLines( 'testfile' )

-Mike
 
T

Terry Reedy

Rigga said:
Thanks for your reply, I was under the misunderstanding that I could read
the contents of the file and get the total number of lines the file
contains in one go. Any pointers to how I can count the lines
manually??

Read the file with lines=readlines() instead of read(). Then
len(lines) is your count. If you really must read with all=read(),
then
count = all.count('\n') + (all[-1] != '\n')

Terry J. Reedy
 
M

Michael Geary

Rigga said:
Im struggling to get to grips with Python at the moment, what
I am trying to do is write a routine that will basically list the
current directory (which contains sub folders) and store all
folder names in an array or text file and then split the each
folder name out as fileds (and assign them to variables) and
then run an external program (from within python) and pass
it fields as input to it..

Be sure to check out the os.walk() function. Scroll to the end of this page:

http://www.python.org/doc/current/lib/os-file-dir.html

-Mike
 
R

Rigga

Michael said:
You actually can do the whole thing in one line of code:

print len( file('testfile').readlines() )

I think that leaves the file open until the file object gets garbage
collected, though, so it's better to explicitly close the file as you did
in your original code.

Also, readlines() reads the entire file into a list, which of course will
use enough memory to hold the entire file. Of course, depending on what
else you're doing with the file, it may be handy to have all of the file
lines in a list. But if you just want to get the line count with minimal
memory overhead, you can write your own loop:

def countLines( filename ):
lines = 0
f = file( filename )
while f.readline():
lines += 1
f.close()
return lines

print countLines( 'testfile' )

-Mike
Thank you for your help its appreciated.

Im struggling to get to grips with Python at the moment, what I am trying to
do is write a routine that will basically list the current directory (which
contains sub folders) and store all folder names in an array or text file
and then split the each folder name out as fileds (and assign them to
variables) and then run an external program (from within python) and pass
it fields as input to it..

Early days I guess but im having fun!

Cheers

Rigga
 

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
474,166
Messages
2,570,907
Members
47,448
Latest member
DeanaQ4445

Latest Threads

Top