13

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?

MattDMo
  • 100,794
  • 21
  • 241
  • 231
Filipe Teixeira
  • 479
  • 1
  • 7
  • 26

1 Answers1

20
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.

jez
  • 14,867
  • 5
  • 37
  • 64
  • 3
    As a matter of fact, `open` has been preferred over `file` since Python 2.5 (source: https://docs.python.org/release/2.5/lib/built-in-funcs.html ). Python 2.5 was released in September, 2006. That's over eight years ago. –  Jan 24 '15 at 01:59