Finding a file's directory address on a Mac

后端 未结 4 912
我在风中等你
我在风中等你 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 10:07

    The desktop is just a subdirectory of the user’s home directory. Because the latter is not fixed, use something like os.path.expanduser to keep the code generic. For example, to read a file called somefile.txt that resides on the desktop, use

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

    If you want this to be portable across operating systems, you have to find out where the desktop directory is located on each system separately.

提交回复
热议问题