Loop filling a set with logging

After watching Raymond Hettingers talk Transforming Code into Beautiful, Idiomatic Python I got back to a function I wrote.

I'm not quite sure how to make it more pythonic but I think this might be a use-case to use the map function.

import logging
import string
import os

def _mounted_drives(possible_device_letters=string.ascii_uppercase):
"""Returns a set of mounted drive letters on a Windows machine."""
result = set()
for letter in possible_device_letters:
mounted = os.path.exists(letter + ':')
if mounted:
logging.info('Found device ' + letter)

return result

if __name__ == '__main__':
print(_mounted_drives())

-
There shouldn't be a leading underscore in the function name. By convention, a leading underscore would indicate that it is a private member of a class. –  200_success Feb 17 '14 at 19:43

Removing the logging and using set comprehension :

def _mounted_drives(possible_device_letters=string.ascii_uppercase):
return {l for l in possible_device_letters if os.path.exists(l + ':')}


If you do want the logging but you do not care about it being done once every device is found :

def _mounted_drives(possible_device_letters=string.ascii_uppercase):
s = {l for l in possible_device_letters if os.path.exists(l + ':')}
for l in s:
logging.info('Found device ' + l)
return s

-