-2

UPDATED I have an 'Images' folder and inside this folder there are 2 sub folders named 18 and 19

Inside folder 18 & 19 there are images named 'final.png'

I have a location.csv file

number latitude longitude name

18 , 6.984719398 ,79.86861158 ,xyz

19 , 6.984719398 ,79.87759469 ,abc

PROBLEM : I want to convert the name from 'final.png' to 6.984719398_79.86861158_final.png

How can I write a python script or any other program to accomplish this?

Here is the code I tried

import os
import pandas as pd
import csv

df = pd.read_csv("names.csv")
for i, sub_dir in enumerate(df["number"]):
    img_path = os.path.join("images", sub_dir)
    new_name = df["latitude"][i] + '_' + df["longitude"][i]+"_final.png"
    os.rename(os.path.join(img_path, "final.png"),
              os.path.join(img_path, new_name))
Madhuka Harith
  • 175
  • 1
  • 2
  • 13
  • can you show us your attempts? I'm not being strict or anything, but it would be clearer with an in/out example or some code even with holes in it. – Jean-François Fabre Jan 14 '17 at 14:12
  • 3
    The fact that these are image files is irrelevant. I suggest you read the CSV data into a dict, with the number strings as the keys & the letter strings as the values. That will let you quickly look up the correct letters corresponding to the folder name. Take a look at the `os` & `os.path` modules for functions that can list the folder names and a function that can be used to rename files. – PM 2Ring Jan 14 '17 at 14:17
  • @Downvoter : This is a valid question,which has a logical answer. Why would anyone downvote this? That is seriously misleading – Madhuka Harith Jan 15 '17 at 13:07

3 Answers3

1

This should work, assuming that you have the pandas library (http://pandas.pydata.org/) installed:

import os
import pandas as pd

df = pd.read_csv("file.csv")
for i, sub_dir in enumerate(df["sub_directories"]):
    img_path = os.path.join("Images", sub_dir)
    new_name = df["filenames"][i] + "_final.png"
    os.rename(os.path.join(img_path, "final.png"), os.path.join(img_path, new_name))

Note that this code renames the files, it does not copy them in a different location. It also assumes that the subdirectories column of the csv has name "sub_directories" and the filenames column has name "filenames"

Blackecho
  • 1,226
  • 5
  • 18
  • 26
  • I tried this, but it gives me an error "'numpy.int64' object has no attribute 'startswith' " , I ran the script inside the 'images folder' – Madhuka Harith Jan 14 '17 at 15:11
  • We are iterating through all folders in the csv--won't this fail if there are other entries in the CSV that do not exist as a folder? – Owen Hempel Jan 14 '17 at 16:24
  • @OwenHempel did this method work? I think if there are mismatching entries, it would just skip without failing – Madhuka Harith Jan 14 '17 at 16:38
  • @OwenHempel yes, I was assuming that all the entries in the CSV were valid folders to keep the code simple. It's easy to check for the existence of folders using the `os.path.isdir()` function – Blackecho Jan 15 '17 at 09:39
  • @MadhukaHarith you should run the script outside of the "Images" folder or, if you want to run it inside it, you should remove `img_path` from the script and directly use `sub_dir` – Blackecho Jan 15 '17 at 09:42
0

so first off, I would start by doing this without pandas at all. It looks like using pandas is causing your error.

try this:

student_dict = {}
with open(file.csv, 'r') as f:
  for line in f:
  l = line.split(',')
    student_dict[l[0]] = l[1]

At this point you will have a dict of (strings of--this is important) student numbers as keys with the matching name as a value.

Now we can iterate through your folder and rename the file within.

for dir in [name for name in os.listdir(a_dir) if os.path.isdir(os.path.join(current_path, name))]: #this ugly line generates all subdirs in your current dir
  new_name = student_dict[dir] + "_final.png"
  os.rename(os.path.join(img_path, "final.png"), os.path.join(img_path, new_name))
Owen Hempel
  • 434
  • 2
  • 8
0

SOLUTION

Hi, here is the simple solution

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

myfile  = open('names.csv', "rb")
reader = csv.reader(myfile)
path="/home/images/"

rownum = 0
for row in reader:
    # Save header row.
    if rownum == 0:
        header = row //I put this line just to skip it
    else:
        oldPath=path+row[0]+'/'+"final.png"
        newPath=oldPath+row[3]+'_'+"final.png" 
        os.rename(oldPath,newPath) 
    rownum += 1

myfile.close()

EXPLAINED

As I explained in the question I have 'images' folder and I save it on 'path variable'. I also have a name.csv which I save it in 'myFile' variable. In the second iteration of the loop(after header)

row[0]=18 so oldPath="/home/images/18/final.png"

row[3]="abc" so newPath="/home/images/18/abc_final.png"

So after the os.rename() function the file name will become abc_final.png in both folders.

If you found this helpful, please upvote my question and answer :)

Madhuka Harith
  • 175
  • 1
  • 2
  • 13