使用Python中的数组创建具有名称的新文本文件
我对Python已经相当生疏了(我的技能,即使不生疏,充其量也只是初级的),我正在尝试自动创建配置文件。我基本上是尝试获取MAC地址列表(由用户手动输入),并创建文本文件,将这些MAC地址作为它们的名称,最后附加.cfg。我曾尝试过接受用户输入并将其附加到数组中,但我遇到了一个绊脚石。很明显,我正处于这个项目的初级阶段,但这只是一个开始。以下是到目前为止我得到的信息:使用Python中的数组创建具有名称的新文本文件,python,list,file-io,Python,List,File Io,我对Python已经相当生疏了(我的技能,即使不生疏,充其量也只是初级的),我正在尝试自动创建配置文件。我基本上是尝试获取MAC地址列表(由用户手动输入),并创建文本文件,将这些MAC地址作为它们的名称,最后附加.cfg。我曾尝试过接受用户输入并将其附加到数组中,但我遇到了一个绊脚石。很明显,我正处于这个项目的初级阶段,但这只是一个开始。以下是到目前为止我得到的信息: def main(): print('Welcome to the Config Tool!') macTa
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()
def main():
打印('欢迎使用配置工具!')
macTable=[]
numOfMACs=int(输入(“我们今天设置了多少MAC地址?”)
而len(macTable)
可以看出,我试图使用数组并在open命令中使用它作为文件名,而Python不喜欢它
任何帮助都将不胜感激 您正在尝试打开一个列表。你需要像这样的东西:
open(macTable[index], 'w')
您正在尝试打开一个列表。你需要像这样的东西:
open(macTable[index], 'w')
您正在尝试打开一个列表。你需要像这样的东西:
open(macTable[index], 'w')
您正在尝试打开一个列表。你需要像这样的东西:
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)
open(mac, 'w')
macTable.append(mac)
到
然后你需要决定你想在你的macTable中有MAC地址还是文件名。我看到的第一个问题是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)
open(mac, 'w')
macTable.append(mac)
到
然后你需要决定你想在你的macTable中有MAC地址还是文件名。我看到的第一个问题是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)
open(mac, 'w')
macTable.append(mac)
到
然后你需要决定你想在你的macTable中有MAC地址还是文件名。我看到的第一个问题是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)
open(mac, 'w')
macTable.append(mac)
到
然后,您需要决定是否要在您的macTable中包含MAC地址或文件名。首先,您不需要单独的列表来存储用户输入的值,您可以动态创建文件
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()
首先,您不需要单独的列表来存储用户输入的值,您可以动态创建文件
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()
首先,您不需要单独的列表来存储用户输入的值,您可以动态创建文件
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()
首先,您不需要单独的列表来存储用户输入的值,您可以动态创建文件
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()
无法
open(macTable,'r')
,因为open
要求第一个参数为字符串,而不是列表。但是,您可以在macTable:open(fname,'w')中打开fname。您不能打开(macTable,'r')
,因为open
要求第一个参数是字符串,而不是列表。但是,您可以在macTable:open(fname,'w')
中打开fname。您不能打开(macTable,'r')
,因为open
要求第一个参数是字符串,而不是列表。但是,您可以在macTable:open(fname,'w')
中打开fname。您不能打开(macTable,'r')
,因为open
要求第一个参数是字符串,而不是列表。不过,您可以在macTable:open(fname,'w')
中对fname进行。我在while循环中有正确的缩进,格式有点乱,我在将其粘贴到问题中时没有更正它。我在while循环中完成了这项工作,效果很好!现在插入我的配置!有人能告诉我为什么投反对票吗?谢谢你让我知道如何改进我的答案。你没有减少numomfmacs
,这将导致无限while
循环。我尝试过提高你的评论,但显然我没有足够的声誉,它否决了它……我不知道为什么。你的回答很好。@anmol\u uppal,这就是为什么有len(macTable)
-macTable在增长。我在while循环中有正确的缩进,格式有点乱,我在粘贴到问题中时没有更正它。我在while循环中完成了这项工作,效果很好!现在插入我的配置!有人能告诉我为什么投反对票吗?谢谢你让我知道如何改进我的答案。你没有减少numomfmacs
,这将导致无限while
循环。我尝试过提高你的评论,但显然我没有足够的声誉,它否决了它……我不知道为什么。你的回答很好。@anmol\u uppal,这就是为什么有len(macTable)
-macTable在增长。我在while循环中有正确的缩进,格式有点乱,我在粘贴到问题中时没有更正它。我在while循环中完成了这项工作,效果很好!现在插入我的配置!有人能告诉我为什么投反对票吗?谢谢你让我知道如何改进我的答案。你没有减少numomfmacs
,这会导致无限while
循环。我尝试过对你的评论进行投票,但显然我没有