This is something that works for me:
lst = ['Hello There
', 'How are ya
', 'All good?
']
def save():
text_file = asksaveasfile(title="Select Location", filetypes=(("Text Files", "*.txt"),))
for line in lst:
text_file.write(line) # Use str(line) if lst is of integers
The mistake is that asksaveasfile
returns a _io.TextIOWrapper
object, meaning its same as using open(...)
one a file. What you wanted was asksaveasfilename()
which will return the path of the object, then:
def save():
text_file = asksaveasfilename(title="Select Location", filetypes=(("Text Files", "*.txt"),))
with open(text_file, 'w') as f:
for line in lst:
f.write(line) # Use str(line) if list of integers
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…