How to create a file in Python based on input content?

You can use the following code to create a file based on the input content:

# 获取用户输入的文件名和内容
file_name = input("请输入文件名:")
file_content = input("请输入文件内容:")

# 打开文件并写入内容
with open(file_name, "w") as file:

print(f"文件 {file_name} 创建成功")

After running this code, the program will prompt the user to input a file name and its content. It will then create a new file based on the user input and write the content into the file.

Leave a Reply 0

Your email address will not be published. Required fields are marked *