0

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

2
  • 1
    You don't do anything with m Commented Sep 4, 2019 at 5:03
  • f is opening the file, that is what creates the name, m does nothing. You have to add the ".txt" to f, the answer below is the correct version Commented Sep 4, 2019 at 8:42

1 Answer 1

1
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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.