使用 Python 中数组的名称创建新的文本文件
Creating new text files with names from an array in Python
我在 Python 方面相当生疏(我的技能,即使不生疏,充其量也只是初级的),我正在尝试自动创建配置文件。我基本上是在尝试获取 MAC 地址的列表(由用户手动输入),并使用这些 MAC 地址作为名称创建文本文件,并在末尾附加 .cfg。我已经设法偶然发现并接受用户输入并将其附加到一个数组中,但我 运行 变成了一个绊脚石。我显然处于这个项目的初级阶段,但这是一个开始。到目前为止,这是我得到的:
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()
可以看出,我试图获取数组并在打开命令中将其用作文件名,但 Python 不喜欢它。
如有任何帮助,我们将不胜感激!
您正在尝试打开一个列表。你需要这样的东西:
open(macTable[index], 'w')
我看到的第一个问题是 while 循环的缩进。你有:
while len(macTable) < numOfMACs:
mac = input("Enter the MAC of the phone: "+".cfg")
macTable.append(mac)
虽然应该是:
while len(macTable) < numOfMACs:
mac = input("Enter the MAC of the phone: "+".cfg")
macTable.append(mac)
至于文件,您也需要循环打开它们,因此:
for file in macTable:
open(file, 'w')
或者你也可以在这段时间做:
while len(macTable) < numOfMACs:
mac = input("Enter the MAC of the phone: "+".cfg")
macTable.append(mac)
open(mac, 'w')
macTable.append(mac)
您可能想要更改的另一件事是输入处理。我了解到您想从用户那里读取 MAC 地址并将配置文件命名为 <MAC>.cfg
。因此我建议更改
mac = input("Enter the MAC of the phone: "+".cfg")
到
mac = input("Enter the MAC of the phone:")
filename = mac + ".cfg"
然后你需要决定你是否想要 MAC 地址或你的文件名 macTable
首先,您不需要单独的列表来存储用户输入的值,您可以即时创建文件。
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()
但是您可以简单地使用 for
循环到 运行 指定的次数,这将使您的代码更清晰:
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()
我在 Python 方面相当生疏(我的技能,即使不生疏,充其量也只是初级的),我正在尝试自动创建配置文件。我基本上是在尝试获取 MAC 地址的列表(由用户手动输入),并使用这些 MAC 地址作为名称创建文本文件,并在末尾附加 .cfg。我已经设法偶然发现并接受用户输入并将其附加到一个数组中,但我 运行 变成了一个绊脚石。我显然处于这个项目的初级阶段,但这是一个开始。到目前为止,这是我得到的:
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()
可以看出,我试图获取数组并在打开命令中将其用作文件名,但 Python 不喜欢它。
如有任何帮助,我们将不胜感激!
您正在尝试打开一个列表。你需要这样的东西:
open(macTable[index], 'w')
我看到的第一个问题是 while 循环的缩进。你有:
while len(macTable) < numOfMACs:
mac = input("Enter the MAC of the phone: "+".cfg")
macTable.append(mac)
虽然应该是:
while len(macTable) < numOfMACs:
mac = input("Enter the MAC of the phone: "+".cfg")
macTable.append(mac)
至于文件,您也需要循环打开它们,因此:
for file in macTable:
open(file, 'w')
或者你也可以在这段时间做:
while len(macTable) < numOfMACs:
mac = input("Enter the MAC of the phone: "+".cfg")
macTable.append(mac)
open(mac, 'w')
macTable.append(mac)
您可能想要更改的另一件事是输入处理。我了解到您想从用户那里读取 MAC 地址并将配置文件命名为 <MAC>.cfg
。因此我建议更改
mac = input("Enter the MAC of the phone: "+".cfg")
到
mac = input("Enter the MAC of the phone:")
filename = mac + ".cfg"
然后你需要决定你是否想要 MAC 地址或你的文件名 macTable
首先,您不需要单独的列表来存储用户输入的值,您可以即时创建文件。
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()
但是您可以简单地使用 for
循环到 运行 指定的次数,这将使您的代码更清晰:
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()