0

I need to create a file with the name provided by the user as input. For example, If the user-provided value hello then the file name should be hello.txt , here is a sample code to create a new file.

import os
recipeFileName = input()
currentRecipe = open("fileName.txt", "x")
Sudhanshu Patel
  • 759
  • 1
  • 5
  • 12
lavrot
  • 1
  • Does this answer your question? [f-strings vs str.format()](https://stackoverflow.com/questions/43123408/f-strings-vs-str-format) – sushanth Nov 05 '20 at 10:56

2 Answers2

1

Since it is a text file so pls use w instead of x.

recipeFileName = input("Input file Name: ")
currentRecipe = open( recipeFileName + '.txt' , "w")
# insert into file
currentRecipe.close()
Sudhanshu Patel
  • 759
  • 1
  • 5
  • 12
0

open can be used to create a file.(you can just replace the string "fileName.txt" with the veriable containing the string and add '.txt' for the extension

recipeFileName = input()
currentRecipe = open(recipeFileName+'.txt' , "x")
Noa
  • 13
  • 3