你可以得到那麼你的應用程序的進程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