On Tue, Mar 22, 2016 at 10:45 PM, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote: > On Tue, 22 Mar 2016 11:05:01 +0000, BartC <b...@freeuk.com> declaimed the > following: > >> >>But out of interest, how would /you/ write a function that takes a >>file-spec and turns it into an in-memory string? And what would its use >>look like? >> > At the basics -- and letting the garbage collector get the file handle > later... > > imstr = open(fileName, "r").read() > > If you want a separate function... (the name here stinks, but...) > > def fn2str(fileName): > fin = open(fileName, "r") > imstr = fin.read() > fin.close() > return imstr > > ... > data = fn2str("some.file") > > letting any exceptions propagate upwards.
While we're on the subject of Pythonic ways to read files, this is NOT. http://thedailywtf.com/articles/finding-the-file It's also not idiomatic C# code either, though... ChrisA -- https://mail.python.org/mailman/listinfo/python-list