Вопрос задан 25.06.2023 в 00:54. Предмет Информатика. Спрашивает Сенотрусов Паша.

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.
0 0
Перейти к ответам

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

Внимание! Ответы на вопросы дают живые люди. Они могут содержать ошибочную информацию, заблуждения, а также ответы могут быть сгенерированы нейросетями. Будьте внимательны. Если вы уверены, что ответ неверный, нажмите кнопку "Пожаловаться" под ответом.
Отвечает Котик-Злобина Люба.

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

0 0
Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.

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.

0 0

Похожие вопросы

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

Последние заданные вопросы в категории Информатика

Задать вопрос