Python- 创建一个以变量作为名称的文本文件
Python- creating a text file with a variable as a name
所以我正在做一个项目,我的程序创建了一个名为 "Ten Green Bottles" 的文本文件,并在其中写入了 10 green bottles 歌曲,我已经成功地让它工作了,但我想让它变得更好。我首先让用户可以选择瓶子的数量,并且效果很好。现在我只希望名称与用户输入的瓶子数量相关。即,如果我放入 20 个瓶子,我希望文本文件的名称为 "Twenty Green Bottles" 而不是 "Ten Green Bottles"
代码:
num1=int(input('Pick a number between 10 and 30: '))
def main():
numbers =[ 'no', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen', 'Twenty', 'Twentyone', 'Twentytwo', 'Twentythree', 'Twentyfour', 'Twentyfive', 'Twentysix', 'Twentyseven', 'Twentyeight', 'Twentynine', 'Thirty' ]
text_one = 'green bottle%s\nHanging on the wall'
text_two = "\nAnd if one green bottle\nShould accidentally fall\nThere'll be"
text_three =' \n'
with open( 'Green Bottles.txt', 'w') as a:
for l in range(num1, 0, -1):
a.write(numbers[l]+ ' ')
if l == 1:
a.write(text_one % '' +'\n')
else:
a.write(text_one % 's' +'\n')
a.write(numbers[l] + ' ')
if l == 1:
a.write(text_one % '' + '\n')
else:
a.write(text_one % 's' + '\n')
a.write(text_two + ' ')
a.write(numbers[l-1] + ' ')
if (l - 1) ==1 :
a.write(text_one % ''+'\n')
else:
a.write(text_one % 's'+'\n')
a.write('\n' + '\n')
if __name__ == '__main__':
main()
您可以将文件名指定给函数,然后 return 文件名。
def write(nameOfFile):
print('Creating new text file')
name = nameOfFile+'.txt' # Name of text file coerced with +.txt
try:
file = open(name,'r+') # Trying to create a new file or open one
return file
except:
print('Something went wrong! Can\'t tell what?')
sys.exit(0) # quit Python
a = write(numbers[num1] )
替换此行:
with open( 'Green Bottles.txt', 'w') as a:
有
with open(numbers[num1] + ' Green Bottles.txt', 'w') as a:
这会在您的文件名前加上适当的词。
您可以使用字符串格式将您的输入添加到文件名
使用
with open('%s Green Bottles.txt' % numbers[num1], 'w') as a:
所以我正在做一个项目,我的程序创建了一个名为 "Ten Green Bottles" 的文本文件,并在其中写入了 10 green bottles 歌曲,我已经成功地让它工作了,但我想让它变得更好。我首先让用户可以选择瓶子的数量,并且效果很好。现在我只希望名称与用户输入的瓶子数量相关。即,如果我放入 20 个瓶子,我希望文本文件的名称为 "Twenty Green Bottles" 而不是 "Ten Green Bottles" 代码:
num1=int(input('Pick a number between 10 and 30: '))
def main():
numbers =[ 'no', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen', 'Twenty', 'Twentyone', 'Twentytwo', 'Twentythree', 'Twentyfour', 'Twentyfive', 'Twentysix', 'Twentyseven', 'Twentyeight', 'Twentynine', 'Thirty' ]
text_one = 'green bottle%s\nHanging on the wall'
text_two = "\nAnd if one green bottle\nShould accidentally fall\nThere'll be"
text_three =' \n'
with open( 'Green Bottles.txt', 'w') as a:
for l in range(num1, 0, -1):
a.write(numbers[l]+ ' ')
if l == 1:
a.write(text_one % '' +'\n')
else:
a.write(text_one % 's' +'\n')
a.write(numbers[l] + ' ')
if l == 1:
a.write(text_one % '' + '\n')
else:
a.write(text_one % 's' + '\n')
a.write(text_two + ' ')
a.write(numbers[l-1] + ' ')
if (l - 1) ==1 :
a.write(text_one % ''+'\n')
else:
a.write(text_one % 's'+'\n')
a.write('\n' + '\n')
if __name__ == '__main__':
main()
您可以将文件名指定给函数,然后 return 文件名。
def write(nameOfFile):
print('Creating new text file')
name = nameOfFile+'.txt' # Name of text file coerced with +.txt
try:
file = open(name,'r+') # Trying to create a new file or open one
return file
except:
print('Something went wrong! Can\'t tell what?')
sys.exit(0) # quit Python
a = write(numbers[num1] )
替换此行:
with open( 'Green Bottles.txt', 'w') as a:
有
with open(numbers[num1] + ' Green Bottles.txt', 'w') as a:
这会在您的文件名前加上适当的词。
您可以使用字符串格式将您的输入添加到文件名
使用
with open('%s Green Bottles.txt' % numbers[num1], 'w') as a: