Reputation: 125
Let's say I have two variables
name='apple'
sentence='apple is delicious.'
in a cell. Then I write
with open('name.txt', 'a') as fp:
fp.write(sentence)
Then, jupyter notebook produces a text file named 'name.txt' with the sentence. But I want to produce a text file named 'apple.txt' so that everytime I change the value of the variable 'name', I would get txt files with different filenames.
What could I do?
Upvotes: 0
Views: 737
Reputation: 88
you can try this:
with open(name+'.txt', 'a') as fp:
fp.write(sentence)
Upvotes: 1