仅仅是为了练习我在python中编写一个程序,从/ usr / share / application / *中的.desktop条目检查已安装应用程序的版本是否可以读取.desktop文件,就像任何其他文本文件一样?另外对于我正在寻找文件中'version ='条目的版本并读取,直到它是整数结束为例
X-GNOME-Bugzilla-Version=3.8.1
X-GNOME-Bugzilla-Component=logview
所以我希望能够只读到3.8.1而不是下一行
applicationPath = '/usr/share/application'
app = os.listdir(applicationPath)
for package in app:
if os.isfile(package):
fileOb = open(applicationPath+'/'+package,'r')
version = fileOb.read()
elif os.isdir(package):
app_list = os.listdir(applicationPath+'/'+package)
如果可以读取.desktop文件
version = fileOb.read()
^将读取整个文件,我如何才能获得我正在寻找的部分?
答案 0 :(得分:1)
if(checkBoundaries(restaurant, latLng))
个对象产生它们的行,所以:
myViewVariable.inBoundryRestraunts = model.filter(function(r){ return checkBoundaries(r, latLng);})
给出了文件的行。这意味着您可以将程序简单地扩展到:
file
您也可以使用正则表达式,但在这种情况下似乎没有必要。它看起来像是:
for line in f:
坦率地说,我只是使用字符串操作来获取您的版本号
...
if os.isfile(package):
with open(app_path + "/" + package) as f:
# use this idiom instead. It saves you from having to close the file
# and possibly forgetting (or having your program crash first!)
for line in f:
if "-Version=" in line:
version = line # do you want the whole line?
# or just "3.8.1"
break # no reason to read any more lines of the file
...