Python: printing a file to stdout

后端 未结 8 1948
眼角桃花
眼角桃花 2020-12-08 03:55

I\'ve searched and I can only find questions about the other way around: writing stdin to a file :)

Is there a quick and easy way to dump the contents of a file to s

相关标签:
8条回答
  • 2020-12-08 04:23

    If you need to do this with the pathlib module, you can use pathlib.Path.open() to open the file and print the text from read():

    from pathlib import Path
    
    fpath = Path("somefile.txt")
    
    with fpath.open() as f:
        print(f.read())
    

    Or simply call pathlib.Path.read_text():

    from pathlib import Path
    
    fpath = Path("somefile.txt")
    
    print(fpath.read_text())
    
    0 讨论(0)
  • 2020-12-08 04:24

    My shortened version in Python3

    print(open('file.txt').read())
    
    0 讨论(0)
提交回复
热议问题