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))
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"
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))
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 :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.