Iterate directory without joining file with dir (os.join)

Question:

I want to know if there is a package that lets me avoid typing the os.path.join every time I’m opening a file, and have the handler already contain this info.

From this:

import os
top = '~/folder'
for file in os.listdir(top):
    full_file_path = os.path.join(top, file)

To:

import package
top = '~/folder'
for file in package.listdir(top):
    full_file_path = file
Asked By: mkohler

||

Answers:

You can create an alias to simplify the usage of a function:

J = os.path.join

## -or- ##

import from os :: path.join as J


J(top, file)
Answered By: rv.kvetch

You should be able to define an alias on your import:

from os.path import join as pjoin

Or simply import it with from ... import, though join is a bit overloaded as a term, given it’s implication for strings.

from os.path import join
Answered By: Nathaniel Ford

In short: I recommend option 2

Option 1: A potential quick and dirty approach:

If your goal is to then open the files and you don’t want to have to join, a trick could be to change your working directory:

import os
os.chdir(os.path.expanduser('~/folder'))

for file in os.listdir('.'):
    # do something with file

note that file will not contain the full path to your file, but since you changed your local dir (equiv do cd), you will be able to access the file with just its name.

Option 2: Cleaner approach – pathlib:

Aside from that, you can also look into pathlib which is a module that provides nice path utilities.

For example:

import pathlib
path = pathlib.Path('~/folder').expanduser()

for file in path.glob('*'):
    # do something with file

So you get a better idea:

In [23]: path = pathlib.Path('~/Documents').expanduser()
In [24]: [i for i in path.glob('*')]
Out[24]: 
[PosixPath('/home/smagnan/Documents/folder1'),
 PosixPath('/home/smagnan/Documents/folder2'),
 PosixPath('/home/smagnan/Documents/file1')]
Answered By: smagnan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.