Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Directory path EOF
#1
I just want to get a list of images in a directory trying this
from PIL import Image
import os, os.path

imgs = []
path = "C:\Users\me\pictures"
valid_images = [".jpg",".gif",".png",".tga"]
for f in os.listdir(path):
	ext = os.path.splitext(f)[1]
	if ext.lower() not in valid_images:
		continue
imgs.append(Image.open(os.path.join(path,f)))
print (imgs)
but getting this error
    path = "C:\Users\me\pictures\"
                                          ^
SyntaxError: EOL while scanning string literal
Looked at articles on the net but the more I read the more confusing it becomes?
Thanks for looking
Reply
#2
use:
path = "C:/Users/me/pictures/"
Reply
#3
Thanks Larz
I had already tried that, the reason it didn't work for me was that I had the path wrong didn't realise that windows truncated username.
So changed the path and ran the program again with the following result
C:\Users\anybl\Python_Progs\shapes>image.py
[<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=4032x3024 at 0x3C10950>]
What I am after is just a list of the images in a folder i.e
images_0.jpg
images_1.jpg
images_2.jpg
images_3.jpg

I have used this code.
import pathlib

# define the path
currentDirectory = pathlib.Path('.')

for currentFile in currentDirectory.iterdir():  
    print(currentFile)
Which gave me this.
C:\Users\anybl\Python_Progs\shapes>image.py
cat.png
image.py
Shapes.py
Shapes2_Pic.png
Shapes_Pic.png
star.png
Which would be fine if it only gave me the images and not the .py extensions.
Reply
#4
from pathlib import Path
 
# define the path
currentDirectory = Path('.')
 
imagelist = [filename for filename in currentDirectory.iterdir() if filename.suffix == '.png']
for filename in imagelist:
    print(filename)
Reply
#5
Thanks Larz,
Here's what I ended up with
import os
for file in os.listdir("C:/users/anybl/python_progs/shapes/"):
    if file.endswith(".png") or file.endswith(".jpg") or file.endswith(".jpeg"):
        print(file)
Two follow up questions please,
1)How could I, with your solution, print different file types as in mine.
2)How do I place the currentDirectory in my solution instead of a file path.
Happy with everything just as it stands for what I'm doing, which is just playing
but always wanting to learn more.
Reply
#6
>>> from pathlib import Path
>>> cd = Path('.')
>>> imagelist = [filename for filename in cd.iterdir() if filename.suffix in ['.png', '.jpg', '.bmp']]
>>> for filename in imagelist:
...     print(filename)
... 
banner_ext2.png
download_pub.jpg
home_on.png
ico-06.png
logo-2.png
logo.png
navthumb-about.jpg
navthumb-careers.jpg
navthumb-kids.jpg
navthumb-library.jpg
navthumb-news.jpg
navthumb-offices.jpg
view_text.jpg
>>>
Reply
#7
Larz,
Thanks very much for helping me to understand Python a little bit more, as I said in previous post I don't really need any other extension other than .png but the code is now there for future use.
Will go away and look at the solution you have provided until I fully understand what is doing what.
Reply
#8
the list comprehension:
imagelist = [filename for filename in cd.iterdir() if filename.suffix in ['.png', '.jpg', '.bmp']]
can be rewritten like this (to show what's happening):
imagelist = []
for filename in cd.iterdir():
    if filename.suffix == '.png' or \
        filename.suffix == '.jpg', \
        iflename.suffix = '.bmp':
        inagelist.append(filename)
Reply
#9
Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  working directory if using windows path-variable chitarup 2 722 Nov-28-2023, 11:36 PM
Last Post: chitarup
  create a default path with idle to a specific directory greybill 0 873 Apr-23-2023, 04:32 AM
Last Post: greybill
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,199 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  In put path and directory Led_Zeppelin 1 2,468 Apr-23-2021, 03:25 AM
Last Post: Larz60+
  How do I set the path to my module one directory below my python 3.7.3 program? deac33 1 2,010 Jul-30-2019, 04:13 AM
Last Post: Larz60+
  Directory path gahhon 3 2,947 Mar-03-2019, 11:39 PM
Last Post: DeaD_EyE
  .pth file does not show up in sys.path when configuring path. arjunsingh2908 2 5,736 Jul-03-2018, 11:16 AM
Last Post: arjunsingh2908

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020