Hi guys
I am new to python ..
i was trying to move sequential images from a folder with multiple types of sequences in same folder like
a_bc_01.jpg
a_bc_02.jpg
a_bc_03.jpg
c_de_05.jpg
c_de_06.jpg
etc...
to different folders

import os, glob, os.path
import array
path = 'd:\del'

for file in glob.glob( os.path.join(path, '*.jpg') ):
  (dirName, fileName) = os.path.split(file)
  (fileBaseName, fileExtension)=os.path.splitext(fileName)
  print fileBaseName

i can't put them into array and break the loop in between different sequence.


Thanks in advance.

import os, glob
path = 'd:\del'

for my_file in glob.glob( os.path.join(path, '*.jpg') ):
  (dirName, fileName) = os.path.split(my_file)
  (fileBaseName, fileExtension) = os.path.splitext(fileName)
  print fileBaseName

Are you getting errors? You don't need to import os.path, as you can simply use os.path after importing os...

Also, you aren't using the array module so I removed that as well.

Another thing: file is a reserved word in Python so I would suggest that you avoid using it as a variable name.

Finally: Paths in Windows are tricky because '\' is an escape character in Python... so a path such as 'C:\My Documents\Music' should be written as either 'C:\\My Documents\\Music' or r'C:\My Documents\Music' (which is raw string format)

HI
Thanks for your reply
well i am not getting error..right now ,
I am stuck with going further, i got the list of all file in the folder,
but how do i break loop where a particular sequence is finished..
and create another loop with new sequence in the folder.
can you guide me how to go about it..

Thanks

One way to split a path_list into two at selected filenames ...

import os

# let's assume your path list looks like this ...
path_list = ['d:\\del\\a_bc_01.jpg',
'd:\\del\\a_bc_02.jpg', 'd:\\del\\a_bc_03.jpg',
'd:\\del\\c_de_05.jpg', 'd:\\del\\c_de_06.jpg']

# create two empty path lists
path_list1 = []
path_list2 = []
for path in path_list:
    fname = os.path.basename(path)
    #print fname  # test
    # select the filenames to be in each path list
    if fname.startswith('a_bc'):
        path_list1.append(path)
    elif fname.startswith('c_de'):
        path_list2.append(path)

print(path_list1)
print(path_list2)

"""
my result -->
['d:\\del\\a_bc_01.jpg', 'd:\\del\\a_bc_02.jpg', 'd:\\del\\a_bc_03.jpg']
['d:\\del\\c_de_05.jpg', 'd:\\del\\c_de_06.jpg']
"""

Now you can iterate these separated lists and put them into different folders or whatever.

Thanks a lot for your input
is there any way to break the sequence by there number as sometime the filename is same like :
a_bc701.jpg
a_bc702.jpg
a_bc703.jpg
a_bc708.jpg
a_bc709.jpg
a_bc710.jpg
how can i break list in this case .
thanks again for your time and effort.

You can write a function to split the list according to an arbitrary criterion

def cut_list(thelist, criterion):
    """ cut_list(list, function) -> list of lists
    splits a list into sublists according to a given criterion.
    Here a criterion is a function of 2 arguments
        func(item1, item2) -> True if the list must be cut between 2 items else False
    """
    if not thelist;
        return []
    result = [[thelist[0]]]
    for i, item in enumerate(thelist[1:]):
        previous = thelist[i-1]
        if criterion(previous, item):
            result.append([item])
        else:
            result[-1].append(item)
    return result

Next we must write a criterion function for the cut. First write a function to split your image names into prefix and number

import re
pattern = re.compile(r"(\d+)\.jpg")
def split_name(name):
    """split_name(name) -> a pair of strings (prefix, num)
    for exemple split_name("a_bc127.jpg") -> ("a_bc", "127")"""
    match = pattern.search(name)
    return (name[:match.start()], match.group(1))

Then we write a criterion function to cut our list

def cut_rule(name1, name2):
    prefix1, num1 = split_name(name1)
    prefix2, num2 = split_name(name2)
    return prefix1 != prefix2 # return True if the 2 prefixes are different

You can modify the cut rule as you wish. Now, assuming you have a list thenames of the files' basenames, you can call

pieces = cut_list(thenames, cut_rule)

to obtain a list of lists.

This article has been dead for over six months. Start a new discussion instead.