Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 doesn't have the file function

In python 2.7 I was able to do:

file('text.txt', 'w').write('some text')

But in python 3 I have to use the open function, so I cannot write to a file on a single line anymore.

f = open('text.txt', 'w')
print('some text', file = f)
f.close()

Why did they remove the file function?

like image 386
Filipe Teixeira Avatar asked Jan 24 '15 01:01

Filipe Teixeira


1 Answers

open('text.txt', 'w').write('some text')

works the same way and open has been the canonical way to open a file (and hence create a file instance) for a long time, even on Python 2.x.

like image 171
jez Avatar answered Oct 25 '22 19:10

jez