2009-05-18 3 views

ответ

2

Предполагая, что вы находитесь на Mac, вы можете использовать PyObjC.

Вот пример чтения из списка свойств, от Using Python For System Administration, слайд 27.

from Cocoa import NSDictionary 

myfile = "/Library/Preferences/com.apple.SoftwareUpdate.plist" 
mydict = NSDictionary.dictionaryWithContentsOfFile_(myfile) 

print mydict["LastSuccessfulDate"] 

# returns: 2009-08-11 08:38:01 -0600 

И пример записи в PLIST (что я писал):

#!/usr/bin/env python 

from Cocoa import NSDictionary, NSString 

myfile = "~/test.plist" 
myfile = NSString.stringByExpandingTildeInPath(myfile) 

mydict = {"Nice Number" : 47, "Universal Sum" : 42} 
mydict["Vector"] = (10, 20, 30) 
mydict["Complex"] = [47, "i^2"] 
mydict["Truth"] = True 

NSDictionary.dictionaryWithDictionary_(mydict).writeToFile_atomically_(myfile, True) 

Когда я затем запустить defaults read ~/test в Баш, я получаю:

{ 
    Complex =  (
     47, 
     "i^2" 
    ); 
    "Nice Number" = 47; 
    Truth = 1; 
    "Universal Sum" = 42; 
    Vector =  (
     10, 
     20, 
     30 
    ); 
} 

И файл выглядит очень приятно, когда открывается в Property List Editor.app.

Смежные вопросы