0

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!

1
  • You can't open(macTable, 'r') because open expects a string for the first argument, not a list. You can for fname in macTable: open(fname, 'w'), though. Commented May 14, 2015 at 14:05

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

10 Comments

I had the correct indentation in the while loop, The formatting just went kinda screwy and I didn't correct it while pasting it into my question. I did it within the while loop which works nicely! Now onto inserting my config!
Can someone please inform me why the downvote? Thanks for letting me know how to improve my answers.
you are not decrementing numOfMACs which would result in an infinite while loop.
I tried upvoting your comment, but apparently I don't have enough reputation, and it downvoted it.....I'm not sure why. Your answer worked nicely.
@anmol_uppal, That's why there is the len(macTable) - the macTable is growing.
|
1

You're trying to open a list. You need something like:

open(macTable[index], 'w')

Comments

1

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.