Easiest way to read/write a file's content in Python

后端 未结 9 2146
走了就别回头了
走了就别回头了 2020-12-01 17:32

In Ruby you can read from a file using s = File.read(filename). The shortest and clearest I know in Python is

with open(filename) as f:
    s =          


        
9条回答
  •  渐次进展
    2020-12-01 18:26

    If you're open to using libraries, try installing forked-path (with either easy_install or pip).

    Then you can do:

    from path import path
    s = path(filename).bytes()
    

    This library is fairly new, but it's a fork of a library that's been floating around Python for years and has been used quite a bit. Since I found this library years ago, I very seldom use os.path or open() any more.

提交回复
热议问题