forked from ehanson8/file-management-python-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
renameFiles.py
53 lines (46 loc) · 1.57 KB
/
renameFiles.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import time
from datetime import datetime
import argparse
import pandas as pd
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--filename', help='the CSV file of name changes.')
parser.add_argument('-m', '--makeChanges', help='Enter "true" if script \
should actually rename files. Oherwise, it only \
creates a log of the expected file name changes')
args = parser.parse_args()
if args.filename:
filename = args.filename
else:
filename = input('Enter the CSV file of name changes: ')
if args.makeChanges:
makeChanges = args.makeChanges
else:
makeChanges = input('Enter "true" for script to actually rename files')
startTime = time.time()
df = pd.read_csv(filename)
logList = []
for count, row in df.iterrows():
row = row
dir = row['fileLocation']
file = row['file']
newFilename = row['newFile']
oldPath = os.path.join(dir, file)
row['oldPath'] = oldPath
newPath = os.path.join(dir, newFilename)
row['newPath'] = newPath
if makeChanges == 'true':
os.rename(oldPath, newPath)
row['changed'] = 'True'
else:
print('log of expected file name changes created - no files renamed')
row['changed'] = 'False'
logList.append(row)
log = pd.DataFrame.from_dict(logList)
print(log.head(15))
dt = datetime.now().strftime('%Y-%m-%d %H.%M.%S')
log.to_csv('logOfEditingMetadataByItemID_'+dt+'.csv', index=False)
elapsedTime = time.time() - startTime
m, s = divmod(elapsedTime, 60)
h, m = divmod(m, 60)
print('Total script run time: ', '%d:%02d:%02d' % (h, m, s))