tags:

views:

46

answers:

1

I'm just doing a bunch of Python exercises and there is an exercise where you should. given a directory name, iterate over the its 'special files' (containing the pattern _\w+_) and output their absolute paths.

Here's my code:

def get_special_paths(dir):
  filenames = os.listdir(dir)

  for filename in filenames:
    if re.search(r'__\w+__', filename):
      print os.path.abspath(os.path.join(dir, filename))

I joined the dir and filename as suggest in the examples but I don't see while the join() is needed. If I don't join the filename + dir, and instead pass abspath() only the filename, the output would be the same.

+4  A: 

If I don't join the filename + dir, and instead pass abspath() only the filename, the output would be the same.

Only if dir equals the current working directory, which is not necessarily the case. Either you need the join, or get_special_paths should not take an argument, and instead assume dir = os.getcwd().

Piet Delport
+1 That's it. Only tried out the example by using . as the given dir.
Helper Method