Jesus
Jesus

Reputation: 755

Python os listdir not working properly in Mac

I am trying to list all folders of a directory using python in Mac os 10.8.2.

My code is as simple as this:

#!/usr/bin/env python
#coding: utf8
import os
print [name for name in os.listdir(os.path.abspath('.')) if os.path.isdir(name)]

When I run this script in a path where there are folders the outcome is:

[]

I try this same script in another computer (ubuntu) and it works perfectly, listing all the directories in the current path.


I have tried exactly the same in the python console in that path and it works, the outcome is:

['Final', 'Script_actualizacion', 'Z_L1']

What could be happening?

Upvotes: 1

Views: 3014

Answers (1)

Jesus
Jesus

Reputation: 755

Ok now this is working:

#!/usr/bin/env python
#coding: utf8
import os
path = os.path.abspath('.')
print [name for name in os.listdir(path) if os.path.isdir(os.path.join(path,name))]

The problem happened because I was calling the python from a different location so it checked the dirs in the wrong path.

Upvotes: 2

Related Questions