ashil
ashil

Reputation: 97

Problem creating a text file with name provided by input

i am creating a txt file based on user input,But it creates files without any format, i am new to python can anyone help me..

import os
name = input("Name :")
roll = input("Roll No :")
branch = input("Branch :")

add ="name :" + name + "\n" +"Roll No :" + roll +"\n" +"Branch :"+branch

f = open(name,"a+")

m = os.path.join(name +".txt")
f.write(add)

want to get name + ".txt" file for each user

output now is name

Upvotes: 0

Views: 71

Answers (1)

MurrayW
MurrayW

Reputation: 393

import os
name = input("Name :")
roll = input("Roll No :")
branch = input("Branch :")

add ="name :" + name + "\n" +"Roll No :" + roll +"\n" +"Branch :"+branch

f = open(name + ".txt","a+") # This is where you are opening the file

# Below line is not contributing to the posted code at all
# m = os.path.join(name +".txt")

f.write(add)
f.close()

Upvotes: 1

Related Questions