I’m building a Python ISO generation app, and I’m getting some wierd output from os.path.isdir(). I’m running Arch Linux with Python 2.7.1.
I have the following folder structure:
/home/andrew/create_iso/Raw_Materials/
/home/andrew/create_iso/Raw_Materials/test_cd/
[andrew@Cydonia Raw_Materials]$ ls -l
total 4
drwxr-xr-x 3 andrew andrew 4096 Feb 23 10:20 test_cd
As you can see, test_cd/ is a normal Linux folder. However, when I run os.path.isdir(), I get different results depending on whether it’s part of my for loop or whether I hard code it.
import os
>>>for folders in os.listdir('/home/andrew/create_iso/Raw_Materials/'):
... os.path.isdir(folders)
False
>>>os.path.isdir('/home/andrew/create_iso/Raw_Materials/test_cd')
True
I thought maybe there was something wierd in the output I’m getting from os.listdir(), but that also seems to check out:
>>>os.listdir('/home/andrew/create_iso/Raw_Materials/')
['test_cd']
Any idea why it’s treating these cases different? Thanks in advance.
‘test_cd’ by itself is not a directory. You need to do an
os.path.jointo get the absolute path to the directory, and then callisdiron that.