-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdel_all_spaces.py
42 lines (30 loc) · 919 Bytes
/
del_all_spaces.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
import os
import re
import argparse
'''
To do list:
logging
more exception handling
subfolders?
comments
'''
def list_files(dir):
filelist = [file for file in os.listdir(dir) if os.path.isfile(os.path.join(dir, file))]
return filelist
def replace_spaces(filelist):
for file in filelist:
replace = re.sub(r' ', '_', file)
if file != replace:
os.rename(file, replace)
def parse_args():
parser = argparse.ArgumentParser(description='Replace all spaces in filenames with underscores.')
parser.add_argument(dest='dir', action='store', help='Directory in which filenames should be processed')
args = parser.parse_args()
return args
args = parse_args()
try:
os.chdir(args.dir)
files = list_files(args.dir)
replace_spaces(files)
except FileNotFoundError as e:
print('Specified direstory does not exist.\nError message: %s' % e)