在Mac上查找文件的目录地址 [英] Finding a file's directory address on a Mac

查看:131
本文介绍了在Mac上查找文件的目录地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Macbook编程python.我想知道的是如何使用Python的文件功能访问某些文件.谷歌搜索失败了我.

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.

例如,Windows应该是这样的:

For example, Windows would be something like this:

f = open(r'C:\text\somefile.txt')

如何从Mac桌面上保存的文件夹访问某些内容?

How would I access something from a folder saved on the Desktop of a Mac?

推荐答案

桌面只是用户主目录的子目录.由于后者不是固定的,因此请使用os.path.expanduser之类的代码来保持代码的通用性.例如,要读取驻留在桌面上的名为somefile.txt的文件,请使用

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.

这篇关于在Mac上查找文件的目录地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆