Search code examples
pythonjupyter-notebookworking-directory

How to access file names from a folder in a jupyter notebook


I am working with jupyter notebook and python, and I have a folder called 'images', with the images inside titled, 'image0', 'image1', 'image2', etc. I would like to access this folder, and see the largest image number inside the folder. How do I access the folder to see the names of the images inside?

I tried:

imagesList = []
    for image in images:
       imagesList.append(image)

imageNum = []
for image in images:
    imageNum.append(int(image[5:]))
max = 0 
for item in imageNum:
    if item>max:
        max = item
print(max)

but am getting 'images is not defined'.

I also tried:

for image in home/jovyan/images:

but this gave me 'home' is not defined. How do I access the image names within this folder?

Thanks!


Solution

  • import os
    folder_files = os.listdir('images') #You can also use full path.
    print("This Folder contains {len_folder} file(s).".format(len_folder=len(folder_files)))
    
    for file in folder_files:
        #Action with these files
        print(file)