简体   繁体   中英

How to find a folder path in mac os x using python?

I am new to Mac OS X (10.10.4). I want to write a code by using python 2.7, which first I have to check if a folder "Pictures" exists, and follow by saving a file into it. However, I dunno the folder path.

 folder = "~/usr/my_name/Pictures"

However, after I run

path.exists(folder)

The result is false.

What should is the path I should set to folder?

The ~ character signifying the home directory is a shell convention, and doesn't just work outright in Python.

Use the os.path.expanduser() function to have it interpreted like the shell would:

import os.path

folder = os.path.expanduser("~/usr/my_name/Pictures")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM