Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Reading text file with wierd file extension?

Reply
Thread Tools

Reading text file with wierd file extension?

 
 
Lionel
Guest
Posts: n/a
 
      02-02-2009
Hi Folks, Python newbie here.

I'm trying to open (for reading) a text file with the following
filenaming convension:

"MyTextFile.slc.rsc"

My code is as follows:

Filepath = "C:\\MyTextFile.slc.rsc"
FileH = open(Filepath)

The above throws an IOError exception. On a hunch I changed the
filename (only the filename) and tried again:

Filepath = "C:\\MyTextFile.txt"
FileH = open(Filepath)

The above works well. I am able to open the file and read it's
contents. I assume to read a file in text file "mode" the parameter is
scanned for a ".txt" extension, otherwise the Python runtime doesn't
know what version of "open(...)" to invoke. How do I pass the original
filename (MyTextFile.slc.rsc) and get Python to open it as a text
file? Thanks in advance everyone!
 
Reply With Quote
 
 
 
 
Mike Driscoll
Guest
Posts: n/a
 
      02-02-2009
On Feb 2, 12:36*pm, Lionel <lionel.ke...@gmail.com> wrote:
> Hi Folks, Python newbie here.
>
> I'm trying to open (for reading) a text file with the following
> filenaming convension:
>
> "MyTextFile.slc.rsc"
>
> My code is as follows:
>
> Filepath = "C:\\MyTextFile.slc.rsc"
> FileH = open(Filepath)
>
> The above throws an IOError exception. On a hunch I changed the
> filename (only the filename) and tried again:
>
> Filepath = "C:\\MyTextFile.txt"
> FileH = open(Filepath)
>
> The above works well. I am able to open the file and read it's
> contents. I assume to read a file in text file "mode" the parameter is
> scanned for a ".txt" extension, otherwise the Python runtime doesn't
> know what version of "open(...)" to invoke. How do I pass the original
> filename (MyTextFile.slc.rsc) and get Python to open it as a text
> file? Thanks in advance everyone!


The extension shouldn't matter. I tried creating a file with the same
extension as yours and Python 2.5.2 opened it and read it no problem.
I tried it in IDLE and with Wing on Windows XP. What are you using?
What's the complete traceback?

Mike
 
Reply With Quote
 
 
 
 
Simon Brunning
Guest
Posts: n/a
 
      02-02-2009
2009/2/2 Lionel <>:
> Hi Folks, Python newbie here.
>
> I'm trying to open (for reading) a text file with the following
> filenaming convension:
>
> "MyTextFile.slc.rsc"


Some kind of a resource fork, perhaps? Where did the file come from?

Python doesn't do anything magic with filenames, so this must be some
sort of a fileysytem oddity, I think.

--
Cheers,
Simon B.
 
Reply With Quote
 
Lionel
Guest
Posts: n/a
 
      02-02-2009
On Feb 2, 10:41*am, Mike Driscoll <kyoso...@gmail.com> wrote:
> On Feb 2, 12:36*pm, Lionel <lionel.ke...@gmail.com> wrote:
>
>
>
>
>
> > Hi Folks, Python newbie here.

>
> > I'm trying to open (for reading) a text file with the following
> > filenaming convension:

>
> > "MyTextFile.slc.rsc"

>
> > My code is as follows:

>
> > Filepath = "C:\\MyTextFile.slc.rsc"
> > FileH = open(Filepath)

>
> > The above throws an IOError exception. On a hunch I changed the
> > filename (only the filename) and tried again:

>
> > Filepath = "C:\\MyTextFile.txt"
> > FileH = open(Filepath)

>
> > The above works well. I am able to open the file and read it's
> > contents. I assume to read a file in text file "mode" the parameter is
> > scanned for a ".txt" extension, otherwise the Python runtime doesn't
> > know what version of "open(...)" to invoke. How do I pass the original
> > filename (MyTextFile.slc.rsc) and get Python to open it as a text
> > file? Thanks in advance everyone!

>
> The extension shouldn't matter. I tried creating a file with the same
> extension as yours and Python 2.5.2 opened it and read it no problem.
> I tried it in IDLE and with Wing on Windows XP. What are you using?
> What's the complete traceback?
>
> Mike- Hide quoted text -
>
> - Show quoted text -


Hi Mike,

maybe it's not a "true" text file? Opening it in Microsoft Notepad
gives an unformatted view of the file (text with no line wrapping,
just the end-of-line square box character followed by more text, end-
of-line character, etc). Wordpad opens it properly i.e. respects the
end-of-line wrapping. I'm unsure of how these files are being
generated, I was just given them and told they wanted to be able to
read them.

How do I collect the traceback to post it?
 
Reply With Quote
 
Lionel
Guest
Posts: n/a
 
      02-02-2009
On Feb 2, 11:20*am, Lionel <lionel.ke...@gmail.com> wrote:
> On Feb 2, 10:41*am, Mike Driscoll <kyoso...@gmail.com> wrote:
>
>
>
>
>
> > On Feb 2, 12:36*pm, Lionel <lionel.ke...@gmail.com> wrote:

>
> > > Hi Folks, Python newbie here.

>
> > > I'm trying to open (for reading) a text file with the following
> > > filenaming convension:

>
> > > "MyTextFile.slc.rsc"

>
> > > My code is as follows:

>
> > > Filepath = "C:\\MyTextFile.slc.rsc"
> > > FileH = open(Filepath)

>
> > > The above throws an IOError exception. On a hunch I changed the
> > > filename (only the filename) and tried again:

>
> > > Filepath = "C:\\MyTextFile.txt"
> > > FileH = open(Filepath)

>
> > > The above works well. I am able to open the file and read it's
> > > contents. I assume to read a file in text file "mode" the parameter is
> > > scanned for a ".txt" extension, otherwise the Python runtime doesn't
> > > know what version of "open(...)" to invoke. How do I pass the original
> > > filename (MyTextFile.slc.rsc) and get Python to open it as a text
> > > file? Thanks in advance everyone!

>
> > The extension shouldn't matter. I tried creating a file with the same
> > extension as yours and Python 2.5.2 opened it and read it no problem.
> > I tried it in IDLE and with Wing on Windows XP. What are you using?
> > What's the complete traceback?

>
> > Mike- Hide quoted text -

>
> > - Show quoted text -

>
> Hi Mike,
>
> maybe it's not a "true" text file? Opening it in Microsoft Notepad
> gives an unformatted view of the file (text with no line wrapping,
> just the end-of-line square box character followed by more text, end-
> of-line character, etc). Wordpad opens it properly i.e. respects the
> end-of-line wrapping. I'm unsure of how these files are being
> generated, I was just given them and told they wanted to be able to
> read them.
>
> How do I collect the traceback to post it?- Hide quoted text -
>
> - Show quoted text -


Sorry, I forgot to mention I'm using Python 2.5 and the Python IDLE.
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-02-2009
> Hi Mike,
>
> maybe it's not a "true" text file? Opening it in Microsoft Notepad
> gives an unformatted view of the file (text with no line wrapping,
> just the end-of-line square box character followed by more text, end-
> of-line character, etc). Wordpad opens it properly i.e. respects the
> end-of-line wrapping. I'm unsure of how these files are being
> generated, I was just given them and told they wanted to be able to
> read them.



Sounds like a unix-line-ended textfile. Python has no problems reading
these, and frankly I'm a bit wondering why you have that IOError -but
then, on windows *anything* is possible.

You can try to open the file using the "rb" flag, which opens it in binary.

However, Python doesn't care about any extensions, that must be
something else you did different.

> How do I collect the traceback to post it?


Copy and paste from the console or wherever you run the script?

Diez
 
Reply With Quote
 
Mike Driscoll
Guest
Posts: n/a
 
      02-02-2009
On Feb 2, 1:20*pm, Lionel <lionel.ke...@gmail.com> wrote:
> On Feb 2, 10:41*am, Mike Driscoll <kyoso...@gmail.com> wrote:
>
>
>
> > On Feb 2, 12:36*pm, Lionel <lionel.ke...@gmail.com> wrote:

>
> > > Hi Folks, Python newbie here.

>
> > > I'm trying to open (for reading) a text file with the following
> > > filenaming convension:

>
> > > "MyTextFile.slc.rsc"

>
> > > My code is as follows:

>
> > > Filepath = "C:\\MyTextFile.slc.rsc"
> > > FileH = open(Filepath)

>
> > > The above throws an IOError exception. On a hunch I changed the
> > > filename (only the filename) and tried again:

>
> > > Filepath = "C:\\MyTextFile.txt"
> > > FileH = open(Filepath)

>
> > > The above works well. I am able to open the file and read it's
> > > contents. I assume to read a file in text file "mode" the parameter is
> > > scanned for a ".txt" extension, otherwise the Python runtime doesn't
> > > know what version of "open(...)" to invoke. How do I pass the original
> > > filename (MyTextFile.slc.rsc) and get Python to open it as a text
> > > file? Thanks in advance everyone!

>
> > The extension shouldn't matter. I tried creating a file with the same
> > extension as yours and Python 2.5.2 opened it and read it no problem.
> > I tried it in IDLE and with Wing on Windows XP. What are you using?
> > What's the complete traceback?

>
> > Mike- Hide quoted text -

>
> > - Show quoted text -

>
> Hi Mike,
>
> maybe it's not a "true" text file? Opening it in Microsoft Notepad
> gives an unformatted view of the file (text with no line wrapping,
> just the end-of-line square box character followed by more text, end-
> of-line character, etc). Wordpad opens it properly i.e. respects the
> end-of-line wrapping. I'm unsure of how these files are being
> generated, I was just given them and told they wanted to be able to
> read them.
>
> How do I collect the traceback to post it?


The traceback should look something like this fake one:

Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
raise IOError
IOError

Just copy and paste it in your next message. The other guys are
probably right in that it is a line ending issue, but as they and I
have said, Python shouldn't care (and doesn't on my machine).

Mike
 
Reply With Quote
 
Lionel
Guest
Posts: n/a
 
      02-02-2009
On Feb 2, 12:10*pm, Mike Driscoll <kyoso...@gmail.com> wrote:
> On Feb 2, 1:20*pm, Lionel <lionel.ke...@gmail.com> wrote:
>
>
>
>
>
> > On Feb 2, 10:41*am, Mike Driscoll <kyoso...@gmail.com> wrote:

>
> > > On Feb 2, 12:36*pm, Lionel <lionel.ke...@gmail.com> wrote:

>
> > > > Hi Folks, Python newbie here.

>
> > > > I'm trying to open (for reading) a text file with the following
> > > > filenaming convension:

>
> > > > "MyTextFile.slc.rsc"

>
> > > > My code is as follows:

>
> > > > Filepath = "C:\\MyTextFile.slc.rsc"
> > > > FileH = open(Filepath)

>
> > > > The above throws an IOError exception. On a hunch I changed the
> > > > filename (only the filename) and tried again:

>
> > > > Filepath = "C:\\MyTextFile.txt"
> > > > FileH = open(Filepath)

>
> > > > The above works well. I am able to open the file and read it's
> > > > contents. I assume to read a file in text file "mode" the parameter is
> > > > scanned for a ".txt" extension, otherwise the Python runtime doesn't
> > > > know what version of "open(...)" to invoke. How do I pass the original
> > > > filename (MyTextFile.slc.rsc) and get Python to open it as a text
> > > > file? Thanks in advance everyone!

>
> > > The extension shouldn't matter. I tried creating a file with the same
> > > extension as yours and Python 2.5.2 opened it and read it no problem.
> > > I tried it in IDLE and with Wing on Windows XP. What are you using?
> > > What's the complete traceback?

>
> > > Mike- Hide quoted text -

>
> > > - Show quoted text -

>
> > Hi Mike,

>
> > maybe it's not a "true" text file? Opening it in Microsoft Notepad
> > gives an unformatted view of the file (text with no line wrapping,
> > just the end-of-line square box character followed by more text, end-
> > of-line character, etc). Wordpad opens it properly i.e. respects the
> > end-of-line wrapping. I'm unsure of how these files are being
> > generated, I was just given them and told they wanted to be able to
> > read them.

>
> > How do I collect the traceback to post it?

>
> The traceback should look something like this fake one:
>
> Traceback (most recent call last):
> * File "<pyshell#3>", line 1, in <module>
> * * raise IOError
> IOError
>
> Just copy and paste it in your next message. The other guys are
> probably right in that it is a line ending issue, but as they and I
> have said, Python shouldn't care (and doesn't on my machine).
>
> Mike- Hide quoted text -
>
> - Show quoted text -


Okay, I think I see what's going on. My class takes a single parameter
when it is instantiated...the file path of the data file the user
wants to open. This is of the form "someFile.slc". In the same
directory of "someFile.slc" is a resource file that (like a file
header) contains a host of parameters associated with the data file.
The resource file is of the form "someFile.slc.rsc". So, when the user
creates an instance of my class which, it invokes the __init__ method
where I add the ".rsc" extension to the original filename/path
parameter that was passed to the class "constructor". For example:

Note: try-catch blocks ommitted.

class MyUtilityClass:
def __init__(self, DataFilepath):
Resourcepath = DataFilepath + ".rsc"
DataFileH = open(DataFilepath)
ResourceFileH = open(Resourcepath)


Invoking this from the Python shell explicitly is no problem i.e.
"TestH = open("C:\\TestResourceFile.slc.rsc") works. BUT...something
is lost when I append the ".rsc" extension to the DataFilePath
parameter as above. When the __init__ method is invoked, Python will
open the data file but generates the exception with the "open
(Resourcepath)" instruction. I think it is somehow related to the
backslashes but I'm not entirely sure of this. Any ideas?

Thanks for the help folks.
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-02-2009
Lionel schrieb:
> On Feb 2, 12:10 pm, Mike Driscoll <kyoso...@gmail.com> wrote:
>> On Feb 2, 1:20 pm, Lionel <lionel.ke...@gmail.com> wrote:
>>
>>
>>
>>
>>
>>> On Feb 2, 10:41 am, Mike Driscoll <kyoso...@gmail.com> wrote:
>>>> On Feb 2, 12:36 pm, Lionel <lionel.ke...@gmail.com> wrote:
>>>>> Hi Folks, Python newbie here.
>>>>> I'm trying to open (for reading) a text file with the following
>>>>> filenaming convension:
>>>>> "MyTextFile.slc.rsc"
>>>>> My code is as follows:
>>>>> Filepath = "C:\\MyTextFile.slc.rsc"
>>>>> FileH = open(Filepath)
>>>>> The above throws an IOError exception. On a hunch I changed the
>>>>> filename (only the filename) and tried again:
>>>>> Filepath = "C:\\MyTextFile.txt"
>>>>> FileH = open(Filepath)
>>>>> The above works well. I am able to open the file and read it's
>>>>> contents. I assume to read a file in text file "mode" the parameter is
>>>>> scanned for a ".txt" extension, otherwise the Python runtime doesn't
>>>>> know what version of "open(...)" to invoke. How do I pass the original
>>>>> filename (MyTextFile.slc.rsc) and get Python to open it as a text
>>>>> file? Thanks in advance everyone!
>>>> The extension shouldn't matter. I tried creating a file with the same
>>>> extension as yours and Python 2.5.2 opened it and read it no problem.
>>>> I tried it in IDLE and with Wing on Windows XP. What are you using?
>>>> What's the complete traceback?
>>>> Mike- Hide quoted text -
>>>> - Show quoted text -
>>> Hi Mike,
>>> maybe it's not a "true" text file? Opening it in Microsoft Notepad
>>> gives an unformatted view of the file (text with no line wrapping,
>>> just the end-of-line square box character followed by more text, end-
>>> of-line character, etc). Wordpad opens it properly i.e. respects the
>>> end-of-line wrapping. I'm unsure of how these files are being
>>> generated, I was just given them and told they wanted to be able to
>>> read them.
>>> How do I collect the traceback to post it?

>> The traceback should look something like this fake one:
>>
>> Traceback (most recent call last):
>> File "<pyshell#3>", line 1, in <module>
>> raise IOError
>> IOError
>>
>> Just copy and paste it in your next message. The other guys are
>> probably right in that it is a line ending issue, but as they and I
>> have said, Python shouldn't care (and doesn't on my machine).
>>
>> Mike- Hide quoted text -
>>
>> - Show quoted text -

>
> Okay, I think I see what's going on. My class takes a single parameter
> when it is instantiated...the file path of the data file the user
> wants to open. This is of the form "someFile.slc". In the same
> directory of "someFile.slc" is a resource file that (like a file
> header) contains a host of parameters associated with the data file.
> The resource file is of the form "someFile.slc.rsc". So, when the user
> creates an instance of my class which, it invokes the __init__ method
> where I add the ".rsc" extension to the original filename/path
> parameter that was passed to the class "constructor". For example:
>
> Note: try-catch blocks ommitted.
>
> class MyUtilityClass:
> def __init__(self, DataFilepath):
> Resourcepath = DataFilepath + ".rsc"
> DataFileH = open(DataFilepath)
> ResourceFileH = open(Resourcepath)
>
>
> Invoking this from the Python shell explicitly is no problem i.e.
> "TestH = open("C:\\TestResourceFile.slc.rsc") works. BUT...something
> is lost when I append the ".rsc" extension to the DataFilePath
> parameter as above. When the __init__ method is invoked, Python will
> open the data file but generates the exception with the "open
> (Resourcepath)" instruction. I think it is somehow related to the
> backslashes but I'm not entirely sure of this. Any ideas?


This is written very slowly, so you can read it better:

Please post the traceback.

Diez
 
Reply With Quote
 
Lionel
Guest
Posts: n/a
 
      02-02-2009
On Feb 2, 1:07*pm, "Diez B. Roggisch" <de...@nospam.web.de> wrote:

This is written very slowly, so you can read it better:

Please post without sarcasm.


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

ResourceFilepath = DataFilepath + ".src"

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

Console output when invoking as "someObject = C8DataType("C:\
\C8Example1.slc")" :

C:\C8Example1.slc
C:\C8Example1.slc.src
[Errno 2] No such file or directory: 'C:\\C8Example1.slc.src'
Error opening C:\C8Example1.slc.src


Thank you


> Lionel schrieb:
>
>
>
>
>
> > On Feb 2, 12:10 pm, Mike Driscoll <kyoso...@gmail.com> wrote:
> >> On Feb 2, 1:20 pm, Lionel <lionel.ke...@gmail.com> wrote:

>
> >>> On Feb 2, 10:41 am, Mike Driscoll <kyoso...@gmail.com> wrote:
> >>>> On Feb 2, 12:36 pm, Lionel <lionel.ke...@gmail.com> wrote:
> >>>>> Hi Folks, Python newbie here.
> >>>>> I'm trying to open (for reading) a text file with the following
> >>>>> filenaming convension:
> >>>>> "MyTextFile.slc.rsc"
> >>>>> My code is as follows:
> >>>>> Filepath = "C:\\MyTextFile.slc.rsc"
> >>>>> FileH = open(Filepath)
> >>>>> The above throws an IOError exception. On a hunch I changed the
> >>>>> filename (only the filename) and tried again:
> >>>>> Filepath = "C:\\MyTextFile.txt"
> >>>>> FileH = open(Filepath)
> >>>>> The above works well. I am able to open the file and read it's
> >>>>> contents. I assume to read a file in text file "mode" the parameter is
> >>>>> scanned for a ".txt" extension, otherwise the Python runtime doesn't
> >>>>> know what version of "open(...)" to invoke. How do I pass the original
> >>>>> filename (MyTextFile.slc.rsc) and get Python to open it as a text
> >>>>> file? Thanks in advance everyone!
> >>>> The extension shouldn't matter. I tried creating a file with the same
> >>>> extension as yours and Python 2.5.2 opened it and read it no problem..
> >>>> I tried it in IDLE and with Wing on Windows XP. What are you using?
> >>>> What's the complete traceback?
> >>>> Mike- Hide quoted text -
> >>>> - Show quoted text -
> >>> Hi Mike,
> >>> maybe it's not a "true" text file? Opening it in Microsoft Notepad
> >>> gives an unformatted view of the file (text with no line wrapping,
> >>> just the end-of-line square box character followed by more text, end-
> >>> of-line character, etc). Wordpad opens it properly i.e. respects the
> >>> end-of-line wrapping. I'm unsure of how these files are being
> >>> generated, I was just given them and told they wanted to be able to
> >>> read them.
> >>> How do I collect the traceback to post it?
> >> The traceback should look something like this fake one:

>
> >> Traceback (most recent call last):
> >> * File "<pyshell#3>", line 1, in <module>
> >> * * raise IOError
> >> IOError

>
> >> Just copy and paste it in your next message. The other guys are
> >> probably right in that it is a line ending issue, but as they and I
> >> have said, Python shouldn't care (and doesn't on my machine).

>
> >> Mike- Hide quoted text -

>
> >> - Show quoted text -

>
> > Okay, I think I see what's going on. My class takes a single parameter
> > when it is instantiated...the file path of the data file the user
> > wants to open. This is of the form "someFile.slc". In the same
> > directory of "someFile.slc" is a resource file that (like a file
> > header) contains a host of parameters associated with the data file.
> > The resource file is of the form "someFile.slc.rsc". So, when the user
> > creates an instance of my class which, it invokes the __init__ method
> > where I add the ".rsc" extension to the original filename/path
> > parameter that was passed to the class "constructor". For example:

>
> > Note: try-catch blocks ommitted.

>
> > class MyUtilityClass:
> > * * def __init__(self, DataFilepath):
> > * * * * Resourcepath *= DataFilepath + ".rsc"
> > * * * * DataFileH * * = open(DataFilepath)
> > * * * * ResourceFileH = open(Resourcepath)

>
> > Invoking this from the Python shell explicitly is no problem i.e.
> > "TestH = open("C:\\TestResourceFile.slc.rsc") works. BUT...something
> > is lost when I append the ".rsc" extension to the DataFilePath
> > parameter as above. When the __init__ method is invoked, Python will
> > open the data file but generates the exception with the "open
> > (Resourcepath)" instruction. I think it is somehow related to the
> > backslashes but I'm not entirely sure of this. Any ideas?

>
> This is written very slowly, so you can read it better:
>
> Please post the traceback.
>
> Diez- Hide quoted text -
>
> - Show quoted text -


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wierd behavior with text box postback =?Utf-8?B?TWljaGFlbCBMb3VnaHJ5?= ASP .Net 3 06-22-2005 02:15 PM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM
Wierd: Can't get .text value of textbox VB Programmer ASP .Net 3 08-31-2004 03:35 AM
Wierd Results when reading content between tags Naren Java 0 05-11-2004 02:04 PM
Wierd problem reading fields in a tab-delimitted row... Kurt Euler Ruby 0 08-24-2003 04:53 AM



Advertisments