5 ms·
My first impression is that this guy is 10 years late! And I agree that it's funny for a Java programmer to be able to read a file with open('bleh').read() ins
by d0m 16y ago
My first impression is that this guy is 10 years late!
And I agree that it's funny for a Java programmer to be able to read a file with open('bleh').read() instead of that 15 stream buffer stream reader stream whatever line of code.
- vidar 16y agoSurely late is better than never.
- jbail 16y agoI am admittedly late to the party. I went to a university where they taught Java (and unfortunately not Python). For the last 10 years, Java has paid the bills, and it still does, so I'm not knocking Java. I stuck with it for so long because I felt that it took that much real world usage to get a good grip on the language. But you are right --- I'm definitely not the first developer to have had that "moment of zen" when opening files in Python, but hopefully I'm also not the last.
- d0m 16y agoSo welcome :) In a couple of months, if you really want to have that "moment of zen" again, give a chance to SICP (it's a wonderful book) which will teach you functional programming and Scheme in the same time.
- Luyt 16y ago...and if you're processing text files line by line, it isn't even necessary to call read() or readlines() on the file, since a file object supports iteration. You could say: for line in open("todo.txt"): # do something with line
- rbanffy 16y agoYou could also [ do_something_to(line) for line in open('todo.txt') ]
- xiongchiamiov 16y agoAlthough you'd probably want to replace those square brackets with parens to make it a generator, if you're dealing with a largish file.
- rbanffy 16y agoIndeed. Hope it's not a huge file.
- elblanco 16y agoI never really understood how the developers and maintainers of the Java language could put up with the overly complicated disaster that is file I/O. They've managed to take something that's relatively simple in most languages and turn it into an utter disaster. I was shocked, years ago, when I was learning Java after having lived in C++ world.
- ShardPhoenix 16y agoDefault Java File I/O is pretty horrible but like a lot of things about Java, the widely-used Apache Commons makes it better: String fileContents = FileUtils.readFileToString(new File("blah")); Still not quite as concise as Ruby/Python/etc though.