Академический Документы
Профессиональный Документы
Культура Документы
Azamat Kibekbaev
Работа с фай лами
• open(file, mode)
• one - 1 - I
• two - 2 - II
• three - 3 - III
• four - 4 - IV
• five - 5 - V
• >>> f1 = open('data.txt')
• >>> f1.readline()
• 'one - 1 - I\n‘
• >>> f1.readline()
• 'two - 2 - II\n'
• >>> f1 = open('data.txt')
• >>> f1.readlines()
• >>> for i in open('data.txt'):
• ... print(i)
• one - 1 - I
• two - 2 - II
• three - 3 - III
• four - 4 - IV
• five - 5 – V
• >>> nums = []
• >>> for i in open('data.txt'):
• ... nums.append(i[:-1])
• ...
• >>> nums
• ['one - 1 - I', 'two - 2 - II', 'three - 3 - III', 'four - 4 - IV', 'five - 5 - V']
• # read the entire file as one string
• with open('filename.txt') as f:
• data = f.read()
• # This will create an error that the file 'filename' doesn't exist.