Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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)
            result.add(letter)

    return result

if __name__ == '__main__':
    print(_mounted_drives())
share|improve this question
    
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 at 19:43

1 Answer 1

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.