two questions - no common theme

S

Sean McIlroy

And now for a pair of questions that are completely different:

1) I'd like to be able to bind callbacks to presses of the arrow
buttons on the keyboard. How do you say that in Tkinter?

2) The function 'listdir' in os.path returns a list of all the files
in the given directory - how do I get hold of a list of its
subdirectories?

Any help will be greatly appreciated.

Peace,
STM
 
W

wittempj

os.walk gives an object with all files in a tree, e.g.
f = os.walk('c:\\temp')
for i in f:
print i
 
S

suryaprakashg

def walktree(self,top=".", depthfirst=False):
"""
Walk a directory tree, starting from 'top'

This code is based on os.path.walk, with the callback function
replaced by a yield and recursion replaced by iteration
"""
if type(top) != types.StringType:
raise TypeError("top must be a string")

# decide which end of the stack to pop
if depthfirst:
index = -1
else:
index = 0

dirstack = [top]
while dirstack:
top = dirstack.pop(index)
try:
names = os.listdir(top)
except os.error:
return
yield top, names
dirs = []
for name in names:
name = os.path.join(top, name)
try:
st = os.lstat(name)
except os.error:
continue
if stat.S_ISDIR(st.st_mode):
dirs.append(name)
# the depth-first results look 'backwards' to me
# so I'm changing them to fit my expectations
if depthfirst:
dirs.reverse()
dirstack.extend(dirs)


this function might be useful
 
F

Fredrik Lundh

Sean said:
1) I'd like to be able to bind callbacks to presses of the arrow
buttons on the keyboard. How do you say that in Tkinter?

<Up>, <Down>, <Left>, <Right>

http://effbot.org/books/tkinterbook/events-and-bindings.htm

For an ordinary 102-key PC-style keyboard, the special keys are Cancel
(the Break key), BackSpace, Tab, Return(the Enter key), Shift_L (any
Shift key), Control_L (any Control key), Alt_L (any Alt key), Pause,
Caps_Lock, Escape, Prior (Page Up), Next (Page Down), End, Home,
Left, Up, Right, Down, Print, Insert, Delete, F1, F2, F3, F4, F5, F6,
F7, F8, F9, F10, F11, F12, Num_Lock, and Scroll_Lock.
2) The function 'listdir' in os.path returns a list of all the files
in the given directory - how do I get hold of a list of its
subdirectories?

listdir returns files and directories; use os.path.isdir(path) to check if path is
a directory:

for name in os.listdir(path):
p = os.path.join(path, name)
if os.path.isdir(p):
...

</F>
 
L

Larry Bates

....how do I get hold of a list of its subdirectories?

This does what you asked for:

import os
targetpath='c:\\'
all=os.listdir(targetpath)
subdirs=[x for x in all
if os.path.isdir(os.path.join(targetpath, x))]

But you may need to take a look at os.walk (as others
have suggested) also.

Larry Bates
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top