[Tutor] Creating typed files with Python, BeOS (OT)

Charlie Clark charlie@begeistert.org
Thu, 21 Feb 2002 08:49:31 -0500


>Dear Python people,
>
>How can you tell Python to make a simple text file or to make a kind 
of file 
>with your own kind of extension=3F  I have looked around in books I 
have, and 
>online.  I have only found stuff about how to open and close and 
recieve 
>input from.  I don't know enough about file creation and operating 
systems 
>to know I guess how to tell Python to do it.

Creating typed files depends on the operating system's support for file 
types.
Extensions are not the same as file types!!!

Python will let you create files with any name as long as it is allowd 
by the operating system:
f =3D open("ourfile", "w")

Giving textfiles a ".txt" extension is purely a convention required for 
primitive file systems such as FAT/ NTFS or Posix. Removing or changing 
the extension can make such files unreadable on that system.

Some file systems such as the BFS provide sophisticated file typing and 
Python can hook into this. Bear in mind that this is platform specific

The following code creates an e-mail file in BeOS

from BeOS.fsattr import write=5Fattr

newfile =3D "newfile.mail"
# creat an empty file
f =3D open(newfile, "w")
f.close()
#give our new file a file type
write=5Fattr(newfile, "BEOS:TYPE", B=5FMIME=5FSTRING=5FTYPE, "text/x-email\0", 
0)

Our file "newfile.mail" now has the MIME-type "text/x-email". E-mail 
files are a subset of textfiles and can be read by any text editor or 
mail program independent of the file extension.

Charlie