The Haiku/BeOS Tip Server
Tips and tricks for Haiku/BeOS users

Python: Accessing attributes

Contributed by: Scot Hacker
This tip is valid for: Both BeOS and Haiku

Chris Herborth has done a magnificent job porting Python, and building additional Python modules that let you access the BFS database. However, the modules documentation doesn’t give a lot of usage examples, and it isn’t immediately obvious how to access particular attribs with the fsattr module.

The following block will serve the same function as the shell’s catattr command, but in native Python:

 attrs = BeOS.fsattr.read_attrs( path_or_filename ) ThisArtist = attrs.get( "Audio:Artist", ( 0, "No artist" ) )[1] 

The value of “ThisArtist” will now equal the contents of the artist attribute for the MP3 or .ogg file in “path_or_filename”. The example also includes error correction so your script won’t hork if it hits an unattributed file.

Props to Chris for this example.

Posted in Scripting

(comments are closed).