简体   繁体   English

如何在python中输入并将格式更改为txt?

[英]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. I'm trying to take the input in string, but i want it to save the file in text format我是初学者,只是尝试一下。

what i want is, that it creates a file in.txt我想要的是,它创建一个文件 in.txt

like,像,

input: helloWorld输入: helloWorld

output: helloWorld.txt输出: helloWorld.txt

Append ".txt" to the filename the user inputs:将“.txt”附加到用户输入的文件名:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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