-2

I know that there are python scripts that can change a file name But i have a little problem I am trying to bulk upload some images into yahoo store, The names for the images has to be the same as the product id for the server to link the images to the proper product. (I still cant understand why they don't just have a field in the database that you can list the name you have set but anyway) I have all my images in one file for all my products (supplied by my supplier) but the names are something like benj7007.jpg I also have a csv product file that lists the sku number and what the image name is. When I am uploading the products into the yahoo store i am using the SKU number for the product id. I can easily create a text file that will list the SKU and Image name for the group of products i am uploading and use it for python to read from SKU Number 700756 Image benj7007.jpg

I need to change the Image name of all of the images to match the SKU but i am lost as to how to do this. can someone point me in the right direction. Thanks Again, I did search but i couldnt find an solution for this kind of issue

Willies
  • 19
  • 1
  • 4

2 Answers2

0

Renaming files:

import os

os.rename("filename.jpg","newname.jpg")

If you want to read from list and rename

with open("listname") as f:
    for line in f:
         os.rename(line,"newname.jpg")
Evus
  • 400
  • 4
  • 12
  • Evus, I am sorry I am not sure I understand Lets say for example I have saved an excel spreadsheet with two columns, The first is the SKU Number and the second is the image name, And I have all my images in one folder "Product Images" I do not want to rename the list, I want to rename the images, to match the SKU. – Willies Aug 25 '16 at 03:56
  • To simplify this you need to save that excel as csv( or text). So it will become format "number","filename"\n . Then you need read that list line by line. First you read image name, and parse it from csv-file. Then you need parse SKU number on that same line. Then just os.rename – Evus Aug 25 '16 at 03:59
  • Okay, I will admit I am still lost, I have not used python in a while so i need to relearn, Thank You Evus maybe some sleep and fresh eyes in the morning will help me understand. – Willies Aug 25 '16 at 04:35
  • I did find a similar question that may resolve my problem is this what you where talking about http://stackoverflow.com/questions/20033458/python-rename-files-reading-names-from-csv-file – Willies Aug 25 '16 at 04:48
  • I am not sure why You folks punished me for asking a question.... after all that is what this site is for. I will also mention that I have used a number of examples listed for question like mine but I could not get any to work. mostly because I did not know what version of python was being used when the question was being asked. Evus thank you again for your advice – Willies Aug 27 '16 at 01:17
  • What version of Python you use? I edit my answer according that and i will try be spesific as i could. – Evus Aug 27 '16 at 01:19
  • 3.4, you did fine Evus, but because I was down ranked i cannot ask any new question. – Willies Aug 27 '16 at 01:21
0
import os
import csv
keys{}
with open('product_image.csv',rU) as csvfile:
        reader = csv.reader(csvfile, delimiter = ',')
        for rowDict in reader:
               keys[ rowDict[0] ] = rowDict[1]
               print (rowDict)
for fileName in (os.listdir('.') & keys.keys() ):
    try:
       os.rename(fileName, keys[fileName])
    except:
        print('image was not renamed')

For those of you who are like me and still trying to learn what everything does, in a spreadsheet the first column is the current name of the image the second column is the name you want to change to. saved as a csv with the name product_image.csv. the script will use the csv list to change the names in the file. One note of error that i found is that for some reason the first file in the list will not rename and will be deleted. I don't take credit for the script I scavenged it and updated it to work with 3.4

Willies
  • 19
  • 1
  • 4