有一个函数可以从相对于另一个路径的路径返回绝对路径吗?

huangapple go评论84阅读模式
英文:

Is there a function that returns the absolute path from a path that is relative to another?

问题

我需要根据一个文件中使用的路径来确定文件的位置。最好使用一个例子来说明:

这是目录结构:

/
  foo
    bar.txt
    text.txt
  bar
    main.go

在text.txt文件中,使用./bar.txt引用了bar.txt文件,这是有效的。但是,如果我尝试从main.go中使用filepath.Abs("./bar.txt")来找到./bar.txt的绝对路径,它将返回/bar/bar.txt,因为它假设.是当前工作目录。

根据文档:
Abs返回路径的绝对表示。如果路径不是绝对路径,它将与当前工作目录连接起来,将其转换为绝对路径。

我的问题是,当.相对于text.txt时,如何获取./bar.txt的绝对路径。

对于这个可能有点复杂的问题,我很抱歉,但我找不到更好的方法来展示一个例子。

英文:

I need to be able to work out where a file is based on the path used in a different file. It's probably just better to use an example:

This is the directory structure:

/
  foo
    bar.txt
    text.txt
  bar
    main.go

In the text.txt file, bar.txt is referenced with ./bar.txt which works but if I try and find the absolute path of ./bar.txt from main.go with filepath.Abs("./bar.txt") then it will return /bar/bar.txt because it assumes that . is the current working directory.

From the documentation:
Abs returns an absolute representation of path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path.

My question is how do I get the absolute path of ./bar.txt when . is realtive to text.txt.

Sorry for this question being probably overcomplicated but I couldn't come up with a better way of showing an example.

答案1

得分: 0

我不确定是否有更简单的方法来使用Go标准库找到绝对路径。你可以使用以下代码来解决问题。

它从第一个文件(text.txt)获取内容,然后检查路径是否已经是绝对路径,如果是绝对路径,则不做任何操作;如果不是绝对路径,则根据text.txt文件找到绝对路径。

package main

import (
	"fmt"
	"os"
	"path"
	"path/filepath"
)

func main() {
	var new_abs_path string
	var text_file = "./foo/text.txt"

	b, err := os.ReadFile(text_file)
	check(err)

	new_file_path := string(b)

	if filepath.IsAbs(new_file_path) {
		new_abs_path = new_file_path
	} else {
		text_abs, err := filepath.Abs(text_file)
		check(err)

		new_abs_path, err = filepath.Abs(path.Join(path.Dir(text_abs), new_file_path))
		check(err)
	}
	fmt.Println(new_abs_path)
}

func check(err error) {
	if err != nil {
		panic(err)
	}
}

希望对你有帮助!

英文:

I don't know exactly maybe there is an easier way to find the absolute path using go stdlib. You can use the following piece of code for the solution.

It gets the content from the first file (text.txt) then checks if the path is already absolute path and if it is absolute it does nothing but if it is not absolute path then it finds absolute path based on text.txt file.

package main

import (
	"fmt"
	"os"
	"path"
	"path/filepath"
)

func main() {
	var	new_abs_path string
	var text_file = "./foo/text.txt"

	b, err := os.ReadFile(text_file)
	check(err)

	new_file_path := string(b)

	if filepath.IsAbs(new_file_path) {
		new_abs_path = new_file_path
	} else {
		text_abs, err := filepath.Abs(text_file)
		check(err)
		
		new_abs_path, err = filepath.Abs(path.Join(path.Dir(text_abs), new_file_path))
		check(err)
	}
	fmt.Println(new_abs_path)
}

func check(err error) {
	if err != nil {
		panic(err)
	}
}


答案2

得分: 0

这个答案已经接近正确,但我成功修改它以适应我的需求。

func getAbs(path string, locationPath string) string {
    new_file_path := path
    new_abs_path := path
    if filepath.IsAbs(new_file_path) {
        new_abs_path = new_file_path
    } else {
        new_abs_path, _ = filepath.Abs(filepath.Join(locationPath, path))
        fmt.Println(new_abs_path, locationPath)
    }
    return new_abs_path
}

这段代码的作用是将文件的路径添加到引用它的文件所在的目录中。

例如:

fmt.Println(getAbs("./bar.txt", "/foo")) // foo 是引用 bar.txt 的文件所在的目录
英文:

This answer is almost there but I managed to modify it to work for me.

func getAbs(path string, locationPath string) string {
    new_file_path := path
    new_abs_path := path
    if filepath.IsAbs(new_file_path) {
	    new_abs_path = new_file_path
    } else {
	    new_abs_path, _ = filepath.Abs(filepath.Join(locationPath, path))
	    fmt.Println(new_abs_path, locationPath)
    }
    return new_abs_path
}

This works by just adding the path to the file to the directory that the file that references it is in.

For example:

fmt.Println(getAbs("./bar.txt", "/foo")) //foo is the directory that text.txt is in which references bar.txt is in

huangapple
  • 本文由 发表于 2023年1月8日 20:05:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75047607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定