Reputation: 21
I'm fairly rusty in Python (and my skills, when not rusty, are rudimentary at best), and I'm trying to automate the creation of config files. I'm basically trying to take a list of MAC Addresses (which are manually inputted by the user), and create text files with those MAC Addresses as their names, with .cfg appended to the end. I've managed to stumble around and accept the user input and append it into an array, but I've ran into a stumbling block. I'm obviously in the very infant phase of this program, but it's a start. Here's what I've got so far:
def main():
print('Welcome to the Config Tool!')
macTable = []
numOfMACs = int(input('How many MAC addresses are we provisioning today? '))
while len(macTable) < numOfMACs:
mac = input("Enter the MAC of the phone: "+".cfg")
macTable.append(mac)
open(macTable, 'w')
main()
As can be seen, I'm trying to take the array and use it in the open command as the filename, and Python doesn't like it.
Any help would be greatly appreciated!
Upvotes: 0
Views: 131
Reputation: 8774
The first problem I can see is the indentation of the while loop. You have:
while len(macTable) < numOfMACs:
mac = input("Enter the MAC of the phone: "+".cfg")
macTable.append(mac)
while it should be:
while len(macTable) < numOfMACs:
mac = input("Enter the MAC of the phone: "+".cfg")
macTable.append(mac)
As for the files, you need to open them in a loop too, so either:
for file in macTable:
open(file, 'w')
Or you can do it in the while as well:
while len(macTable) < numOfMACs:
mac = input("Enter the MAC of the phone: "+".cfg")
macTable.append(mac)
open(mac, 'w')
macTable.append(mac)
Another thing you might want to change is the input processing. I understood that you want to read MAC adresses from the user and name the config files <MAC>.cfg
. Thus I suggest to change
mac = input("Enter the MAC of the phone: "+".cfg")
to
mac = input("Enter the MAC of the phone:")
filename = mac + ".cfg"
and then you need to decide if you want to have MAC adresses or the filenames in you macTable
Upvotes: 1
Reputation: 22974
First of all you don't need a separate list to store the values entered by the user you can create the files on the fly.
def main():
print('Welcome to the Config Tool!')
#macTable = []
numOfMACs = int(input('How many MAC addresses are we provisioning today? '))
while numOfMACs:
mac = input("Enter the MAC of the phone: ")
mac += ".cfg" # Adding extension to the file name
with open(mac, 'w') as dummyFile: # creating a new dummy empty file
pass
numOfMACs-=1 #Avoiding the infinite loop
main()
However you can simple use a for
loop to run for specified number of times which mill make your code cleaner:
def main():
print('Welcome to the Config Tool!')
#macTable = []
numOfMACs = int(input('How many MAC addresses are we provisioning today? '))
for i in range(numOfMACs):
mac = input("Enter the MAC of the phone: ")
mac += ".cfg" # Adding extension to the file name
with open(mac, 'w') as dummyFile: # creating a new dummy empty file
pass
main()
Upvotes: 1
Reputation: 2084
You're trying to open a list. You need something like:
open(macTable[index], 'w')
Upvotes: 1