MAC怎么获取文件路径,怎么找到mac电脑的文件全路径

首页 > 时尚 > 作者:YD1662025-05-09 20:50:06

MAC怎么获取文件路径,怎么找到mac电脑的文件全路径(1)

在Mac上,可以使用以下几种方式获取文件路径:

1. 使用相对路径:如果文件在同一文件夹中,可以使用相对路径来获取文件路径。例如,假设当前工作目录为/Users/username/Documents/example.txt,那么可以使用以下代码来获取文件路径:

```swift

let filePath = Bundle.main.path(forResource: "example", ofType: "txt")!

```

2. 使用绝对路径:如果文件不在同一个文件夹中,或者需要访问其他位置的文件,可以使用绝对路径来获取文件路径。例如,假设文件位于/Volumes/My External Drive/Documents/example.txt中,那么可以使用以下代码来获取文件路径:

```swift

let fileURL = URL(fileURLWithPath: "/Volumes/My External Drive/Documents/example.txt")

let filePath = fileURL.path

```

3. 使用NSWorkspace类:如果你想获取当前打开的文档的完整路径,可以使用NSWorkspace类中的contentsOfDirectory方法。例如,假设你想要获取当前打开的文档的完整路径,可以使用以下代码:

```swift

let documentsPath = NSWorkspace.sharedWorkspace.contentsOfDirectory(atPath: NSHomeDirectory(), includingFiles: true)

let selectedDocument = documentsPath.firstObject as! String

let fullFilePath = NSFileManager.defaultManager().urlsForDirectoryObjects(inDomains: .userDomainMask, allowMultiple: false).first!?.path

let documentPath = (fullFilePath?.stringByAddingPercentEncoding(["%20"])!)?.replacingOccurrences(ofString: " ", withString: "")

```

苹果电脑mac文件路径看的方法步骤如下

1.首先在dock中找到finder,通过搜索找到想要查询路径的文件。

2.在launchpad里面找到终端,到处找找,你会找到的,双击打开终端,类似这种样子。

3..把在finder中找到的文件直接拖入终端中,你就可以看见文件路径了。

栏目热文

文档排行

本站推荐

Copyright © 2018 - 2021 www.yd166.com., All Rights Reserved.