Finding a file's directory address on a Mac

后端 未结 4 871
我在风中等你
我在风中等你 2021-02-04 09:22

I am working with a Macbook programming python. What I want to know is how I can access certain files using Python\'s file functions. A google search failed me.

For exam

4条回答
  •  無奈伤痛
    2021-02-04 09:46

    f = open (r"/Users/USERNAME/Desktop/somedir/somefile.txt")
    

    or even better

    import os
    f = open (os.path.expanduser("~/Desktop/somedir/somefile.txt"))
    

    Because on bash (the default shell on Mac Os X) ~/ represents the user's home directory.

提交回复
热议问题