Finding a file's directory address on a Mac

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

    You're working on a Mac so paths like "a/b/c.text" are fine, but if you use Windows in the future, you'll have to change all the '/' to '\'. If you want to be more portable and platform-agnostic from the beginning, you better use the os.path.join operator:

    import os
    
    desktop = os.path.join(os.path.expanduser("~"), "Desktop")
    filePath = os.path.join(desktop, "somefile.txt")
    
    f = open(filePath)
    

提交回复
热议问题