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

后端 未结 9 2149
走了就别回头了
走了就别回头了 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:27

    Slow, ugly, platform-specific... but one-liner ;-)

    import subprocess
    
    contents = subprocess.Popen('cat %s' % filename, shell = True, stdout = subprocess.PIPE).communicate()[0]
    

提交回复
热议问题