简体   繁体   中英

How to take a input in python and change the format into txt?

This is the whole code

#input from the user
Text = input("Enter a String :) : ")

#this portion will create a file
with open(Text, 'w') as f:
    file = f.write("this is a test file")


#loops through

for i in range(1,100):
#this is creating a new file
    a = open(Text, "a")
    b = a.write("\n this will append to the file")
    a.close() 
    # print(i)

#this portion is reading from the file
f = open(Text, "r")
d = f.read()
print(d,end="")
f.close()

I'm trying to take the input in string, but i want it to save the file in text format I'm a beginner, just trying things.

what i want is, that it creates a file in.txt

like,

input: helloWorld

output: helloWorld.txt

Append ".txt" to the filename the user inputs:

#input from the user
Text = input("Enter a String :) : ") + ".txt"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM