0

I've got a directory full of JPG photographs. I want to take the file names of those photographs and end up with the following being printed:

<description>Test. <![CDATA[<img src='.
/files/fantaWP.jpg]>]]></description>

The file name is a variable. I've tried my very best below and i'm nearly there, but I end up with the following output:

<description>Test. <![CDATA[<img src='.
/files/['fantaWP.jpg', 'icon', 'p1.JPG', 'p2.JPG', 'p3.jpg', 'p4.jpg']>]]></description>

Here is my code:

photofileName = []

path='C:\Users\Simon\Desktop\Dir\pics' 

dirList=os.listdir(path)
for fname in dirList:
    photofileName.append(fname)

print photofileName


photoVar = [x for x in photofileName]

itemsInListOne = 3

iterations = itemsInListOne

num = 0

while num < iterations:
        num = num+1
        print ("\<description>Test. <![CDATA[<img src='./files/{}'>]]></description>\n".format(photoVar))

Thank you in advance.

1 Answer 1

3

The following should be enough if I understand you correctly.

for fname in os.listdir(path):
    print("\<description>Test. <![CDATA[<img src='./files/{}'>]]>=</description>\n".format(fname))

Example:

>>> path = "/home/msvalkon/Pictures/Sample Album"
>>> for fname in os.listdir(path):
...     print("\<description>Test. <![CDATA[<img src='./files/{}'>]]>=</description>\n".format(fname))
...     
... 
\<description>Test. <![CDATA[<img src='./files/Costa Rican Frog.jpg'>]]>=</description>

\<description>Test. <![CDATA[<img src='./files/Pensive Parakeet.jpg'>]]>=</description>

\<description>Test. <![CDATA[<img src='./files/Boston City Flow.jpg'>]]>=</description>

>>> 

And the content of the path..

msvalkon@Lunkwill:~/Pictures/Sample Album$ ll
total 1208
drwxrwxr-x 2 msvalkon msvalkon   4096 Apr 19  2012 ./
drwxr-xr-x 7 msvalkon msvalkon  28672 Jan  3 18:27 ../
-rw-rw-r-- 1 msvalkon msvalkon 339773 Dec 13  2009 Boston City Flow.jpg
-rw-rw-r-- 1 msvalkon msvalkon 354633 Dec 13  2009 Costa Rican Frog.jpg
-rw-rw-r-- 1 msvalkon msvalkon 480098 Dec 13  2009 Pensive Parakeet.jpg
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.