简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM