NEWBIE TIP: Reading a file two different ways...from ENGSOL 
Author Message
 NEWBIE TIP: Reading a file two different ways...from ENGSOL

"""
I'm brand new to Python, and while reading the books,
I tend to bread-board stuff just to see how it works.
I figure it's better to share....
If it's of use to other newbies, great.
If it's in error, please post a correction.
Please excuse the formatting.
"""

#------FILE: file_rd.py

# Notice that "foo" indicates you can name the var whatever you care to

class FileRead:                                   # Create the class
    def __init__ (self, foo_filename):            # Name the variable
        self.foo_file = open(foo_filename, 'r')   # Open the file, and name the handle
        while 1:                                  # Do until EOF
            foo_line = self.foo_file.readline()   # Read a line at a time
            if not foo_line:                      # If EOF, bail
                break
            else:
                print foo_line,                   # Else display the line via stdout
        self.foo_file.close()                     # Tidy up and close the file

def readfile(foo_name):                           # Pretty much as above
    foo_file = open(foo_name, 'r')
    while 1:
        foo_line = foo_file.readline()
        if foo_line:                              # Notice we change the order of
            print foo_line,                       # the IF, just because we can
        else:
            break
    foo_file.close()

#----------------------------------------------------

# Try the two ways to read a file.....
# While in a NT4 DOS box, and directory = C:\python21
# My command line is: python tags\file_rd.py
# Which invokes stuff above and the next two lines:

FileRead("c:\\python21\\tags\\cauxd.tex")  # Notice the double \\'s

readfile("c:\\python21\\tags\\cauxd.tex")

#----END: file_rd.py



Wed, 21 Jan 2004 11:59:29 GMT  
 NEWBIE TIP: Reading a file two different ways...from ENGSOL

    ...

Quote:
> If it's in error, please post a correction.
    ...
> def readfile(foo_name):                           # Pretty much as above
>     foo_file = open(foo_name, 'r')
>     while 1:
>         foo_line = foo_file.readline()
>         if foo_line:                              # Notice we change the
order of
>             print foo_line,                       # the IF, just because
we can
>         else:
>             break
>     foo_file.close()

It should work fine, but it's needlessly complicated.  Why not:

def readfile(foo_name):
    foo_file = open(foo_name, 'r')
    for foo_line in foo_file.xreadlines():
        print foo_line,
    foo_file.close()

if one wants to be Finalizationly Correct, or just:

    for foo_line in open(foo_name, 'r').xreadlines():
        print foo_line,

if the potential risk of having a file stay open (for reading) a
little longer than strictly necessary (in implementations that
can't guarantee finalization, such as Jython) is not an issue.

Alex



Wed, 21 Jan 2004 15:55:28 GMT  
 NEWBIE TIP: Reading a file two different ways...from ENGSOL

Quote:

> FileRead("c:\\python21\\tags\\cauxd.tex")  # Notice the double \\'s

> readfile("c:\\python21\\tags\\cauxd.tex")

Windows is perfectly happy to accept "/" as a legal path separator, so

FileRead("c:/python21/tags/cauxd.tex")

readfile("c:/python21/tags/cauxd.tex")

is more readable, and saves a comment 8-).

PJDM
--
Peter Mayne
IBM GSA (but the opinions are mine)
Canberra, ACT, Australia
This post is covered by Sturgeon's Law.



Fri, 23 Jan 2004 20:55:56 GMT  
 NEWBIE TIP: Reading a file two different ways...from ENGSOL
Hi,


[...]

Quote:
>> readfile("c:\\python21\\tags\\cauxd.tex")

> Windows is perfectly happy to accept "/" as a legal path separator, so
[...]
> readfile("c:/python21/tags/cauxd.tex")

> is more readable, and saves a comment 8-).

There's another way: raw strings

readfile(r"c:\python21\tags\cauxd.tex")

markus
--
Defend democrathy and freedom!



Sun, 25 Jan 2004 00:46:27 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. NEWBIE TIP: Displaying the contents of an imported module...from ENGSOL

2. Newbie Tip: Classes and variable passing vs instances from ENGSOL

3. Different ways to read in a file

4. read two sets of data from two spread sheet files to two arrays problem

5. Merge selected columns from two different files into another file

6. one file unit accessing two different files?

7. how to read one file, print to two files

8. Two threads accessing the same file, different access modes

9. Open the same database file in two different work areas

10. Writing two equal files without creating two files in dct

11. list-directed read statement works in different way on different platforms

12. newbie: Some file object tips?

 

 
Powered by phpBB® Forum Software