简体   繁体   中英

How to access files in a subdirectory in python?

I am running a Jupyter Notebook Script. In the directory that that the notebook is in, is a folder that contains another folder that contains 10 folders with some images in those folders. I want to bring all of those images into my script and store them as a variable. How do I access the images?

The operation that will be performed on each image, With i being each image:

images = tensor([Image.open(i)])

you should work relatively. define a function that read all images from root folder and call it with relative path get_images('./')

def get_images(root):
    paths=[]
    for rootdir, subFolders, files in os.walk(root):
        for filename in files:
            file_path = os.path.join(rootdir, filename)
            try:
                i = Image.open(file_path)
                paths.append(i)
            except IOError:
                pass

    return paths

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