вторник, 24 марта 2015 г.

Средства python для работы с файлами / Tools python to work with files

Добрый день,  думая о том как сохранить результат действий из предыдущей программы калькулятор, я решил разобраться в принципах открытия и редактирования текстовых файлов в python.
У меня получилось не совсем то, что хотелось - проблема появилась сразу после создания функций. Записать в файл я смог, а вот как сделать, что бы между введенными данными были пробелы и как сделать автоматический перевод на новую строку я тоже не разобрался.
Ниже код программы, что у меня получилась  -  2 функции.

Первая  - для ввода имени и фамилии.
Вторая - для ввода возраста.

Еще я не смог разобраться как выполнить вывод информации из файл на экран.

Удобный вариант просмотра кода моей программы сохранен тут http://pastie.org/10049967


/


Good afternoon, thinking about how to keep the result of the actions of the previous program, a calculator, I decided to look into the principles of open and edit text files in python.
I got not quite what they wanted - the problem occurs immediately after creation functions. Write to a file I could, but how to do that would be introduced between the data gaps were and how to make automatic translation to a new line I do not understand.
The following code, what I got - 2 function.

The first - to enter a first and last name.
The second - to enter the age.

I still could not figure out how to perform output of information from the file to the screen.

Convenient way to view my program code stored here http://pastie.org/10049967

def input_name (type_name,message,message_error):
    while True:
        try:
            a=open('data.txt', 'a').write(input(message))
            return a
        except(ValueError):
            print(message_error)

name=input_name (str,'enter the name','message_error')
lastname=input_name (str,'enter the last name','message_error')

def input_age (type_age,message,message_error):
    while True:
        try:
            b=open('data.txt', 'a').write(input(message))
            return b
        except (TypeError, ValueError):
            print(message_error)

age=input_age(int,'enter age','message_error')


Открытие файла 

Встроенная функция open открывает файл у этой функции есть множество параметров открытия файла, я использую параметр 'a' - открытие на до запись, информация добавляется в конец файла. 

Запись в файл

Встроенная метод write - с помощью него мы можем записать в файл информацию введенную например с клавиатуры.

/

Opening a File

Built-in open function opens a file in this function is the set of parameters to open a file, I use the option 'a' - opening up to the record, the information is added to the end of the file.

Filing

Built-in method write - with the help of it we can write to the file such information entered from the keyboard.

Комментариев нет:

Отправить комментарий