Reading a file in home directory from another directory

前端 未结 4 1140
忘了有多久
忘了有多久 2021-01-27 11:18

I am trying to read a file in another directory. My current script is in path/to/dir. However, I want to read a file in ~/. I\'m not sure how to do thi

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-27 12:06

    use os.path.expanduser:

    import os
    with open(os.path.expanduser('~/.file')) as f:
        print f.read()
    

提交回复
热议问题