1+ # Mass Extension Changer
2+ # Coded by Zane ZenOokami Blalock
3+ # Essence Of Zen
4+
5+ # This program essentially takes in your entered directory and changes any specified extension
6+ # with another extension specified.
7+
8+ # To-Do =========
9+ # *Add Menu
10+ # *Add more user-proofing
11+ # *Create a GUI?
12+
13+
14+ import os
15+
16+ # Global Variables ===========================
17+ VERSION = "1.3.1" # A variable to call for showing update once menu is made
18+
19+ DIRECTORY = "" # used for the directory url location
20+ DIRECTORY_ITEMS = [] # This is an array to house the items inside the directory
21+
22+ # Functions ===========================
23+ def changeDirectory (): # Function allows to change the Directory
24+ global DIRECTORY
25+
26+ # We ask for the directory
27+ directory = "" # Create an empty string for the directory location
28+ user_input = input ("Please enter directory location: " ) # Have the user input the directory - they can copy and paste from window's explorer
29+ for item in user_input : # If the directory uses backslash (like from window's syntax), change them to forward slash for Python's syntax
30+ if item == '\\ ' :
31+ item = '/'
32+ directory += item
33+
34+ length = len (directory ) # We grab the length of the directory, and use -1 to get the index of the last character
35+ if directory [length - 1 ] != "/" : # If there isn't a / at the end of the directory, add one.
36+ directory += "/"
37+
38+ DIRECTORY = directory # We set the global variable
39+
40+
41+ def printDirectoryItems (directory ): # Prints the items found in the Directory **REDO THIS CODE LATER SINCE UPDATE
42+ global DIRECTORY_ITEMS
43+ directory_items = os .listdir (directory ) # Sets a variable to be an array of items
44+ print ("Printing Files and Folders: " )
45+ for item in directory_items :
46+ print (item )
47+
48+ DIRECTORY_ITEMS = directory_items
49+
50+ def changeExtension (selection , target ):
51+ global DIRECTORY
52+ global DIRECTORY_ITEMS
53+
54+
55+ for file in DIRECTORY_ITEMS :
56+ current_file = os .path .join (DIRECTORY , file ) # We add the folder's url to the file name to create file's url
57+ if selection in current_file :
58+ print (current_file )
59+ new_name = current_file .replace (selection , target )
60+ output = os .rename (current_file , new_name )
61+
62+ def updateDirectory ():
63+ global DIRECTORY
64+ global DIRECTORY_ITEMS
65+
66+ DIRECTORY_ITEMS = os .listdir (DIRECTORY )
67+
68+ # Main Function
69+ def main ():
70+ global DIRECTORY
71+ global DIRECTORY_ITEMS
72+ # Start the program by getting the directory
73+ # Later create a menu
74+
75+ switch = 1
76+ while switch == 1 :
77+ changeDirectory ()
78+ # directory_items = os.listdir(DIRECTORY)
79+ print ()
80+
81+ printDirectoryItems (DIRECTORY )
82+
83+ print ()
84+
85+ print ("Please enter your target extension to change" )
86+ selection = input ("please enter extension such as .txt or .html: " )
87+ print ()
88+ print ("Please enter what extension you wanted to switch it to" )
89+ target = input ("please enter extension different than before: " )
90+
91+ print ()
92+ changeExtension (selection , target )
93+ print ()
94+ updateDirectory ()
95+
96+ answer = input ("Are you finished?: " ).lower ()
97+ loop = 1
98+ while loop == 1 :
99+ if answer == "yes" or answer == "y" :
100+ loop = 0
101+ switch = 0
102+ elif answer == "no" or answer == "n" :
103+ loop = 0
104+ switch = 1
105+ else :
106+ print ("Answer is invalid, try again please." )
107+ loop = 1
108+
109+ main ()
0 commit comments