Search code examples
pythonlistfor-loopglobsequences

for in converting str to list why?


I'm currently building a script to find image sequences and group them together.. my problem is when I get the file names and go to run a "for in" on them they turn into lists... why?? This is my code:

import os, glob

path = r'C:\Users\manley\Desktop\testSeq'

for my_file in glob.glob( os.path.join(path, '*.jpg') ):

    (dirName, fileName) = os.path.split(my_file)
    (fileBaseName, fExt) = os.path.splitext(fileName)
    #print fileName

    pathLists = fileName
    pathList1 = []
    pathList2 = []

    #print pathList
    #print fileName

    for fName in pathLists:
        print fName

The filename = rightSide_020_30.0001.jpg

when the fName prints I get a list of each letter... How do I get the fName to perserve the fileName as a str?


Solution

  • Your code is a little unclear to me but in your case each letter is getting printed because your doing this

    >>> mylist = "abc"
    >>> for m in mylist:
    ...    print m
    ...
    a
    b
    c
    >>>
    

    Your pathlist is a string.

    I think you meant to do this:

    pathList1 = []
    pathList1.append(filename)
    
    for fname in pathList1:
              print fname