你可以得到那么你的应用程序的进程ID询问OS使用lsof
,所有打开的文件,寻找你的进程ID:
from string import *
from os import getpid
from subprocess import check_output, STDOUT
pid = getpid()
lsof = (check_output(['/usr/sbin/lsof', '-p', str(pid)], stderr=STDOUT)).split("\n")
for line in lsof[1:]:
print line
的常规文件将'REG'
型的第五列,[4]
如果你在索引
文件打开内运行的代码可以显示在类似的方式:从 进口串* 从OS进口GETPID从子进口check_output ,STDOUT 进口重新
pid = getpid()
f = open('./trashme.txt', 'w')
f.write('This is a test\n')
lsof = (check_output(['/usr/sbin/lsof', '-p', str(pid)], stderr=STDOUT)).split("\n")
print lsof[0]
for line in lsof[1:]:
if (re.search('trashme', line)): print line
f.close
导致:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python 6995 greg 3w REG 14,2 0 2273252 /Users/greg/Desktop/trashme.txt
[这个问题](http://stackoverflow.com/questions/849172/getting-the-name-of-document-that-used -to-launch-the-application-bundle-on-os-x)对我来说似乎是同样的问题。 – snapshoe 2010-11-22 03:36:46
您好ma3,看起来像是同样的问题,虽然它看起来只支持GUI工具包,所以我在那里运气不好。我打算给Greg的解决方案一个镜头。 – 2010-11-22 05:08:29