-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename_csv.py
32 lines (25 loc) · 1.04 KB
/
rename_csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python3
import sys, os
from subprocess import call
import pandas as pd
"""This script renames files in a directory, following a csv table with two tab separated columns: old_name new_name
Dependencies: Pandas. Please make sure you have a backup of the files, the action is irreversible."""
def read_table(table):
df = pd.read_csv(table, sep="\t")
return df
def rename_headers(old_name, new_name):
call(("mv "+old_name+" "+new_name), shell=True)
def main(csv):
df=read_table(csv)
df.apply(lambda x: rename_headers(x.old_name, x.new_name), axis=1)
if __name__ == "__main__":
try:
csv = sys.argv[1]
except:
print ("Lacking parameters. Usage: python3 rename_csv.py csv_file. Csv file must contain the following headers: old_name new_name separated by tab")
print("This script renames files irreversibly. Please, make sure you have a backup")
i=input("Do you want to continue?: Y/N ")
if i == "Y" or i == "y":
main(csv)
else:
print("Canceled")