Search code examples
pythonfileoperation

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


Solution

  • 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()