0

Writing a script to help with data migration in renaming images. It seems as though when I try to access the variable filename from within the inner-for-loop, it's just printing .DS_Store

See commented lines for example:

#!/usr/bin/env python
import os
import csv

FILE_PATH = '/Users/admin/Desktop/data-migration/images/product'
COUNT = 0

with open('paths_formatted.csv') as csvfile:
    reader = csv.reader(csvfile)

    # Walk the tree.
    for root, directories, files in os.walk(FILE_PATH):

        for filename in files:

            # Join the two strings in order to form the full filepath.
            filePath = os.path.join(root, filename)

            #print(filePath) - this results in the actual file path

            for row in reader:

                #print(filePath) - this results in .DS_Store

                oldFilePath = row[1].strip()
                displayName = row[0].strip()
                colour = row[2].strip()


                if " " in colour:
                    colour = colour.replace(" ", "-")

                slashIndex = oldFilePath.rfind("/")         
                oldFileName = oldFilePath[slashIndex+1:]

                if oldFileName == filename:

                    number = 1;
                    newFileName = displayName + "_" + colour + "-" + str(number) + ".jpg"
                    while os.path.exists(FILE_PATH + leadingPath + newFileName):
                        number = number + 1
                        newFileName = filePath, displayName + "_" + colour + "-" + str(number)

                    os.rename(newFileName)
                    COUNT = COUNT+1

print(COUNT)

Why would this be?

After changing my code as per the comments, to store the results in a list, now the for root, directories, files in os.walk(FILE_PATH): is not being executed.

I verified that the FILE_PATH exists and printed it to console, also that it has contents.

My new code is as follows:

#!/usr/bin/env python
import os
import csv

FILE_PATH = '/Users/admin/Desktop/data-migration/images/product'
COUNT = 0

productInfo = []

with open('paths_formatted.csv') as csvfile:
    reader = csv.reader(csvfile)

    for row in reader:
        productInfo.append(row)

for root, directories, files in os.walk(FILE_PATH):

    for filename in files:

        for info in productInfo:

            displayName = info[0]
            oldFilePath = info[1]
            colour = info[2]

            slashIndex = oldFilePath.rfind("/")         
            oldFileName = oldFilePath[slashIndex+1:]

            if " " in colour:
                colour = colour.replace(" ", "-")

            if oldFileName == filename:

                number = 1;
                newFileName = displayName + "_" + colour + "-" + str(number) + ".jpg"

                while os.path.exists(FILE_PATH + leadingPath + newFileName):
                    number = number + 1
                    newFileName = filePath, displayName + "_" + colour + "-" + str(number) + ".jpg"

                os.rename(newFileName)
                COUNT = COUNT + 1

print(COUNT)
coopwatts
  • 670
  • 1
  • 8
  • 31
  • Everything here is in the same global scope. – juanpa.arrivillaga May 18 '17 at 22:58
  • But likely, your bug is being caused by the fact that you can only iterate over your `reader` once. Once it has been iterated, you either have to `csvfile.seek(0)` or it's like trying to iterate over an empty list. Instead of `.seek`, you could load all the data into a list immediately. – juanpa.arrivillaga May 18 '17 at 23:02
  • @juanpa.arrivillaga please see updated post after trying your recommendation. – coopwatts May 19 '17 at 00:08

0 Answers0