-
Notifications
You must be signed in to change notification settings - Fork 10
/
rename.py
68 lines (59 loc) · 2.17 KB
/
rename.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# rename.py
# This script acts as a find-and-replace option for changing the names of files
# stored in a directory. Original files will be stored in a separate directory.
# Will only work on relevant files (those that have the problematic string).
# Erin Olson erin.daphne.olson@gmail.com
# Current date: 2013/04/12
from glob import glob
from os import makedirs
from os.path import exists
from subprocess import call
# form
print """rename
This script works like a regular find-and-replace option on word processors.
Please use it accordingly!
What is the file directory?
You can drag and drop the files into the Terminal window to fill out this space
WARNING: No individual directory should have a space character
If so, please go back and replace any spaces with underscores
(Yes it is a little ridiculous to have to do this by hand when you have this
script, but otherwise Python can't read it. Sorry!)"""
filedir = raw_input("> ")
if filedir[-1] == ' ':
filedir = filedir.replace(" ", '')
if filedir[-1] != '/':
filedir = filedir + '/'
print"""
What would you like to call the directory for old files?
Default is: 0_old_file_rename/
Press enter to use default"""
olddir = raw_input("> ")
if olddir == '':
olddir = "0_old_file_rename/"
if olddir[-1] != '/':
olddir = olddir + "/"
print "\nPlease type what you would like to find below:"
oldstring = raw_input("> ")
print "\nPlease type what you would like to replace it with below:"
newstring = raw_input("> ")
# check for directory & make a new one
goodname = False
while goodname == False:
if exists(filedir + olddir):
print "Directory already exists!\nPlease pick a new directory name for old labfiles:"
olddir = raw_input("> ")
if olddir[-1] != '/':
olddir = olddir + '/'
else:
goodname = True
makedirs(filedir + olddir)
# make a list of the files
file_list = glob(filedir+"*")
for file in file_list:
if oldstring in file:
# make a set of file names
oldfilename = file.replace(filedir, '')
newfilename = oldfilename.replace(oldstring, newstring)
# move old file to olddir; copy this file into normal dir
call(["mv", file, filedir + olddir + oldfilename])
call(["cp", filedir + olddir + oldfilename, filedir + newfilename])