
Write a Python program that asks user for name of the file and text that file should contain.
Create a text file with given name and fill it with text that entered by user. Input: Enter a file name: sample Enter a text of the file: This file was created at informatics lesson by using Python programming language.

Ответы на вопрос

Code:
name = input("Enter a file name: ") + ".txt"
text = input("Enter a text of the file: ")
with open(name, "w") as file:
file.write(text)
Output:
Enter a file name: abc
Enter a text of the file: 12345
abc.txt file is created with the text: 12345



You can create a Python program to achieve this by using the input()
function to get the file name and text from the user, and then use the open()
function to create and write to the file. Here's a simple example:
python# Get the file name and text from the user
file_name = input("Enter a file name: ")
file_text = input("Enter a text of the file: ")
# Create the file and write the text to it
try:
with open(file_name + '.txt', 'w') as file:
file.write(file_text)
print(f"File '{file_name}.txt' has been created and filled with the provided text.")
except Exception as e:
print(f"An error occurred: {str(e)}")
This program will ask the user for the file name and the text they want to save in the file. It will then create a text file with the given name and write the provided text into it. Make sure to save this code in a .py
file and run it using a Python interpreter.


Похожие вопросы
Топ вопросов за вчера в категории Информатика







Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili