Reading text file with wierd file extension?

Lionel lionel.keene at gmail.com
Tue Feb 3 03:08:49 CET 2009


On Feb 2, 5:40 pm, "Rhodri James" <rho... at wildebst.demon.co.uk> wrote:
> [Quoting restored for reduced
>
> On Mon, 02 Feb 2009 22:33:50 -0000, Lionel <lionel.ke... at gmail.com> wrote:
> > On Feb 2, 2:01 pm, Mike Driscoll <kyoso... at gmail.com> wrote:
> >> On Feb 2, 3:43 pm, Lionel <lionel.ke... at gmail.com> wrote:
> >> > On Feb 2, 1:07 pm, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
>
> >> >> This is written very slowly, so you can read it better:
>
> >> > Please post without sarcasm.
>
> On Usenet?  You'll be wanting single unequivocal answers next!
>
> Seriously though, you had been asked several times for the traceback,
> so that we could stop guessing and tell you for sure what was going
> on, and you hadn't provided it.  Diez's mild sarcasm was not uncalled-
> for.  The fact that you didn't have a traceback partially excuses you,
> but it would have helped if you'd said so.
>
>
>
>
>
>
>
> >> > This is the output from my Python shell:
>
> >> > >>> DatafilePath = "C:\\C8Example1.slc"
> >> > >>> ResourcefilePath = DatafilePath + ".rsc"
> >> > >>> DatafileFH = open(DatafilePath)
> >> > >>> ResourceFh = open(ResourcefilePath)
> >> > >>> DatafilePath
>
> >> > 'C:\\C8Example1.slc'>>> ResourcefilePath
>
> >> > 'C:\\C8Example1.slc.rsc'
>
> >> > It seems to run without trouble. However, here is the offending code
> >> > in my class (followed by console output):
>
> >> > class C8DataType:
>
> >> >     def __init__(self, DataFilepath):
>
> >> >         try:
> >> >             DataFH = open(DataFilepath, "rb")
>
> >> >         except IOError, message:
> >> >             # Error opening file.
> >> >             print(message)
> >> >             return None
>
> You're catching the IOError, presumably so that you can fail
> gracefully elsewhere.  This may not be a particularly good
> idea, and in any case it stops the exception reaching the
> console where it would cause the traceback to be displayed.
> More on this later.
>
> >> >         ResourceFilepath = DataFilepath + ".src"
>
> As other people have pointed out, you've got a typo here.
>
> >> >         print(DataFilepath)
> >> >         print(ResourceFilepath)
>
> >> >         # Try to open resource file, catch exception:
> >> >         try:
> >> >             ResourceFH = open(ResourceFilepath)
>
> >> >         except IOError, message:
> >> >             # Error opening file.
> >> >             print(message)
> >> >             print("Error opening " + ResourceFilepath)
> >> >             DataFH.close()
> >> >             return None
>
> [Huge amounts of text trimmed]
>
> Fair enough, you're catching the IOError so that you can
> ensure that DataFH is closed.  Unfortunately this concealed
> the traceback information, which would have made it more
> obvious to you what people were talking about.  Given that
> this has rather stuffed your C8DataType instance, you
> might want to think about re-raising the exception after
> you've closed DataFH and letting the outer layers deal
> with it in a more appropriate fashion.
>
> Incidentally, this code isn't going to do anything useful
> for you anyway even after you've fixed the typo.  DataFH
> and ResourceFH are both local variables to __init__ and
> will be tossed away when it finishes executing.  If you
> want to use them later, make them self.data_fh and
> self.resource_fh respectively.
>
> (PEP 8 recommends that you use lower_case_with_underscores
> for variable or attribute names, and leave MixedCase for
> class names.)
>
> --
> Rhodri James *-* Wildebeeste Herder to the Masses- Hide quoted text -
>
> - Show quoted text -

Very good comments. I'll be implementing some of your suggestions, to
be sure. Thanks Rhodri.



More information about the Python-list mailing list