-1

I'm new to coding, and I'm trying to write code to let someone create and name a new text document. However, I have no idea how to do this.

I need help on how to use raw_input() to name a file. E.g., if the user enters 'dog', it will create a new .txt file called 'dog.txt'.

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112

2 Answers2

1
name = raw_input('Enter name of text file: ')+'.txt'

Alone with

open(name,'a') or open(name,'w')
Frostie_the_snowman
  • 629
  • 3
  • 9
  • 17
1
file_name = raw_input("Please enter a name:")
new_file = open(file_name + ".txt", "w")
new_file.close()

Those 3 lines will -

  1. Read input from the user.

  2. Open a file for writing ("w" flag) with the given name

  3. Close the file (empty as we didn't write anything to it).

Tom Ron
  • 5,906
  • 3
  • 22
  • 38