4daJKong
4daJKong

Reputation: 2045

How to open a subfolder and find path by using os.listdir

I want to read all images in a sub-folder, named digit_test by os.listdir:

file_pathname = './digit_test'
for filename in os.listdir(file_pathname):
    print(filename)
    img = cv2.imread(file_pathname+'/'+filename)

However, it shows Exception has occurred: FileNotFoundError

[WinError 3] The system cannot find the specified path.: './digit_test'
  File "D:\Python_ex\lenet_5\lenet_5_isvalid.py", line 15, in <module>
    for filename in os.listdir(file_pathname):

the file structure like that,

lenet_5>-my_test.py
        -digit_test-> 0.PNG

I was wondering if it is correponding to the relative path, because when I use absolute path, it can work now.

file_pathname = r'D:\Python_ex\lenet_5\digit_test' 

Besides,

file_pathname = r'.\lenet_5\digit_test'

also can work now.

Actually, it is problem like that, the main folder includes a python file and secondary folder, and the secondary folder includes some imgs, now I just want to read these imgs in this python file. However, if I use a absolute path, it cannot still work when I change the folder name.

Upvotes: 3

Views: 1007

Answers (1)

jupiterbjy
jupiterbjy

Reputation: 3503

You really need to add full error log for better helps, but my guess is, you executed script outside the script's path.

In short, you might have executed script INSIDE lenet_5 first, then on next runs you either did os.chdir or executed script OUTSIDE it, seemingly Python_ex

In terminal, the directory you are currently in terminal become current working directory for any script you run.

>>> import os
>>> os.listdir("./exported")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [WinError 3] 지정된 경로를 찾을 수 없습니다: './exported'

[WinError 3] 지정된 경로를 찾을 수 없습니다: './exported'

# current working directory is not where I intended
>>> os.getcwd()
'C:\\Users\\jupiterbjy'

# now change directory to where I want to be

>>> os.chdir("E:/Media/Works/CSP")
>>> os.listdir("./exported")
['banners', 'bg2.png', 'box.png', ...

>>> os.getcwd()
'E:\\Media\\Works\\CSP'

Old answer

Why not try out much superior alternative pathlib.Path?

if os.listdir is absolutely needed you can just convert it into string and feed the string-fied path into os.listdir.

>>> import pathlib
>>> root = pathlib.Path("./")
>>> root
WindowsPath('.')

>>> root.absolute().as_posix()
'E:/Media/Works/CSP/Exported'

>>> root.joinpath("banners").absolute()
WindowsPath('E:/Media/Works/CSP/Exported/banners')

>>> def images_gen():
...     for path in root.iterdir():
...         if path.suffix in (".png", ".jpg"):
...             yield path

>>> import pprint
>>> images = list(images_gen())
>>> pprint.pprint(images)
[WindowsPath('bg2.png'),
 WindowsPath('box.png'),
...

>>> data = images[0].read_bytes()
>>> len(data)
81245


# pathlib.Path features

>>> pprint.pprint([method for method in dir(root) if not method.startswith("_")])
['absolute',
 'anchor',
 'as_posix',
 'as_uri',
 'chmod',
 'cwd',
 'drive',
 'exists',
 'expanduser',
 'glob',
 'group',
 'hardlink_to',
 'home',
 'is_absolute',
 'is_block_device',
 'is_char_device',
 'is_dir',
 'is_fifo',
 'is_file',
 'is_mount',
 'is_relative_to',
 'is_reserved',
 'is_socket',
 'is_symlink',
 'iterdir',
 'joinpath',
 'lchmod',
 'link_to',
 'lstat',
 'match',
 'mkdir',
 'name',
 'open',
 'owner',
 'parent',
 'parents',
 'parts',
 'read_bytes',
 'read_text',
 'readlink',
 'relative_to',
 'rename',
 'replace',
 'resolve',
 'rglob',
 'rmdir',
 'root',
 'samefile',
 'stat',
 'stem',
 'suffix',
 'suffixes',
 'symlink_to',
 'touch',
 'unlink',
 'with_name',
 'with_stem',
 'with_suffix',
 'write_bytes',
 'write_text']

Upvotes: 2

Related Questions