govin
govin

Reputation: 125

Filename as a variable in the Jupyter notebook

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

Answers (1)

Swe_ba
Swe_ba

Reputation: 88

you can try this:

with open(name+'.txt', 'a') as fp:
        fp.write(sentence)

Upvotes: 1

Related Questions