-
Notifications
You must be signed in to change notification settings - Fork 3
/
addextensions.py
executable file
·44 lines (34 loc) · 1.03 KB
/
addextensions.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
#!/usr/bin/env python
import os
import sys
def print_help( scriptName ):
'''
'''
description = 'Add an extension to all files in a directory tree which do not have an extension yet.'
print description
print
print 'Usage: python ' + scriptName + ' DIRECTORY EXTENSION'
print
#
# entry point
#
if __name__ == "__main__":
# always show the help if no arguments were specified
if len( sys.argv ) < 3:
print_help( sys.argv[0] )
sys.exit( 1 )
directory = sys.argv[1]
newExtension = sys.argv[2]
if not newExtension[0] == '.':
newExtension = '.' + newExtension
for root, dirnames, filenames in os.walk( directory ):
for filename in filenames:
extension = os.path.splitext( filename )[1]
# only use filenames without an extension
if extension:
continue
newFilename = filename + newExtension
fullpath = os.path.join( root, filename )
newFullpath = os.path.join( root, newFilename )
print fullpath + " -> " + newFullpath
os.rename( fullpath, newFullpath )