Python: printing a file to stdout

后端 未结 8 1969
眼角桃花
眼角桃花 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())
    

提交回复
热议问题