简体   繁体   English

如何使用python在Mac OS X中找到文件夹路径?

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

I am new to Mac OS X (10.10.4). 我是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. 我想使用python 2.7编写代码,首先我必须检查文件夹“ Pictures”是否存在,然后将文件保存到其中。 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. 表示主目录的~字符是shell约定,不仅在Python中可以直接使用。

Use the os.path.expanduser() function to have it interpreted like the shell would: 使用os.path.expanduser()函数来像shell那样解释它:

import os.path

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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