1

I have a list of directory name and want to add a fixed path before it. I am aware that, it can be done in two ways, 1) Using for loop and 2) using map() function.

I was able to do using for loop:

dirNames = ['dir1', 'dir2']
dirsFullPath = [os.path.join('C:\\', dir) for dir in dirNames]

But with map how to pass two parameters i.e. 'C:\' and directory name for every directory name in list.

dirsFullPath = map(os.path.join, dirNames) # so where to put 'C:\\' in this line.
Luminos
  • 161
  • 11
  • No this question is completely different. Although we can accomplish the goal using `functools.partial` but this isn't that complex a situation. – Abhinav Dec 16 '15 at 10:09
  • @a_bhi_9 what makes you think that *this question is completely different*? – styvane Dec 16 '15 at 10:11
  • @user3100115, user3100115: One of the answers from "How to do multiple arguments to map function where one remains the same in python?" solves my problem but answer here "How to bind a constant parameter in Python?" is very complicated. Can you remove second link? – Luminos Dec 16 '15 at 10:44
  • After marking as duplicate, its redirecting to this link 'http://stackoverflow.com/questions/14826888/python-os-path-join-on-a-list' , it does not answers my question precisely. – Luminos Dec 16 '15 at 10:50

1 Answers1

3

You need to pass a function and an iterable to map, for application as simple as this, you can rely on a lambda function and the iterable being your list.

import os
dirNames = ['dir1', 'dir2']
path_maker = lambda dir_name: os.path.join("C:\\", dir_name)
map(path_maker, dirNames)

Refer to the documentation of map for further detail, https://docs.python.org/2/library/functions.html#map

Abhinav
  • 938
  • 1
  • 11
  • 23