[Tutor] Copy file function works, but not with adding the date copied

Alan Gauld alan.gauld at btinternet.com
Fri Feb 13 20:11:37 CET 2009


<cclpianos at comcast.net> wrote
>
> I am modifying a simple program "copy file" from the tutorials Alan
> Gauld wrote. In it's first simpler instance (W/O date) it works
> beautifully. But I wanted to
> add the date the file was copied. I've kept it simple by pointing to
> a single file.
>
> The problem lies somewhere in how the program modifies the
> information. It creates the new copied file, but can't open with the
> application TextEdit.

> The date this file was copied Friday February 13
>
> {\rtf1\mac\ansicpg10000\cocoartf824\cocoasubrtf440

The problem is that this is an RTF format which is a binary format.
You can process binary data ion Python (see the box on the files
topic page) but it is much more difficult that normal plain text 
files.

So you have written a line of plain text at the front of the RTF file
and the word processor doesn't know how to interpret that their.
So it ignores it!

> import time
> inp = open( "/Users/pw/Desktop/Terminal_commands.rtf","r") # file to

Open a plain text file (eg foo.txt) instead of an RTF file.

That should work. Unfortunatey reading and writing RTF files is
a much more advanced topic than you might expect, too much
for your level of skill just now. By the time you've completed the
tutorial you might be ready to try :-)

Also I think there is a Python module somewhere for reading/writing
RTF files if that is a real need that you have.

HTH,

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list