File Reading In Mac

Ned Deily nad at acm.org
Tue Nov 23 02:50:07 CET 2010


In article 
<66e4164c-e81d-4a65-b847-c5ef900fa14c at a37g2000yqi.googlegroups.com>,
 dilip raghavan <dilip1984me at gmail.com> wrote:
>          I have been trying to read contents from a file in MAC.
> I wrote the code
> 
> filename = "test.rtf"
> FileHandle = open(filename,'r')
> 
> fileStr = FileHandle.read()
> print fileStr
> FileHandle.close()
> 
> When I see the output I see a lot of junk. The junk is like a lot of
> question marks, the font information and other details of the file.
> The actual content is lost in the junk.
> 
> I have tried other methods like readline but still I see the junk.
> I tried the asme code in windows and it worked correctly.
> Can any one tell me the reason and the solution  for this.

With an extension of "rtf", the file is presumably a "Rich Text Format" 
file.  http://en.wikipedia.org/wiki/Rich_Text_Format

There are some Python packages out there for dealing with rtf format 
files.  You might try rtf2xml to convert the file, preserving style 
attributes:  http://pypi.python.org/pypi/rtf2xml/
Or look at the Mac OS X command line utility textutil (man 1 textutil) 
to convert the file to another format.  Or use the OS X TextEdit.app.

-- 
 Ned Deily,
 nad at acm.org




More information about the Python-list mailing list