comparison python/dmenu.py @ 289:e1a861bbb559

sample file picker
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 10 May 2013 14:25:29 -0700
parents
children 03d62a2cbe62
comparison
equal deleted inserted replaced
288:498210b6e1a9 289:e1a861bbb559
1 #!/usr/bin/env python
2
3 import optparse
4 import os
5 import subprocess
6 import sys
7
8 def choose_file(directory, dmenu='dmenu'):
9 """choose a file in the directory with dmenu"""
10 directory = os.path.abspath(directory)
11 files = os.listdir(directory)
12 string = '\n'.join(files)
13
14
15 process = subprocess.Popen([dmenu, '-i'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
16 stdout, _ = process.communicate(input=string)
17 if process.returncode:
18 return
19 chosen = os.path.join(directory, stdout)
20 if os.path.isdir(chosen):
21 return choose_file(chosen)
22 return chosen
23
24 def main(args=sys.argv[1:]):
25 parser = optparse.OptionParser()
26 print choose_file(os.getcwd())
27
28 if __name__ == '__main__':
29 main()