os.IsNotExist但是得到的是”not a directory”。

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

os.IsNotExist but getting "not a directory"

问题

我无法理解,为什么我不能创建一个文件夹并写入文件,然后再次读取它 - 使用相同的路径和相同的函数,只是为了测试?当我在文件上运行"go test myio_test.go"时,我得到以下结果:

myio_test.go

...
func TestMyIo(t *testing.T) {

	myio.CreateTempJsonFile()
}
....

myio.go

package myio

import (
	"fmt"
	"io/ioutil"
	"path"
	// "syscall"
	// "os"
	"bitbucket.org/kardianos/osext"
	"os"
)

func CreateTempJsonFile() {

	exePath, _ := osext.Executable()
	filePath := path.Dir(exePath + "/json/")
	fmt.Println("create: ", filePath)

	_, errx := os.Stat(filePath)
	if os.IsNotExist(errx) {
		errm := os.Mkdir(filePath, 0644)
		if errm != nil {
			fmt.Println("error creating dir...")
			panic(errm)
		}
	}

	writeError := ioutil.WriteFile(filePath+"/user.txt", []byte(`{"user": "Mac"}`), 0644) // os.ModeTemporary)// 0644)
	if writeError == nil {
		fmt.Println("Ok write")
	} else {
		fmt.Println("Error write")
	}

	_, err := ioutil.ReadFile(filePath + "/user.txt")
	if err == nil {
		fmt.Println("Reading file")
	} else {
		fmt.Println("Error reading file")
	}
}

好像os.Stat(filePath)认为文件夹已经存在。如果我删除对os.Stat()的检查,直接创建文件夹,我会得到一个"not a directory"的错误。

fmt.Println("create: ", filePath)打印的结果是:

/private/var/folders/yj/jcyhsxxj6ml3tdfkp9gd2wpr0000gq/T/go-build627785093/command-line-arguments/_test/myio.test/json

这一切都很奇怪,因为每个测试都会创建一个新的"/go-buildxxx/"文件夹,因此该文件夹实际上不应该存在。

有什么想法吗?

英文:

I can not understand, why I am not able to create a folder and write to a file, and then read it in again - with the same path in same func, just for testing? <br>
When I run "go test myio_test.go" on the file. I get

myio_test.go

...
func TestMyIo(t *testing.T) {

	myio.CreateTempJsonFile()
}
....

myio.go

package myio

import (
	&quot;fmt&quot;
	&quot;io/ioutil&quot;
	&quot;path&quot;
	//&quot;syscall&quot;
	// &quot;os&quot;
	&quot;bitbucket.org/kardianos/osext&quot;
	&quot;os&quot;
)

func CreateTempJsonFile() {

	exePath, _ := osext.Executable()
	filePath := path.Dir(exePath + &quot;/json/&quot;)
	fmt.Println(&quot;create: &quot;, filePath)

	_, errx := os.Stat(filePath)
	if os.IsNotExist(errx) {
		errm := os.Mkdir(filePath, 0644)
		if errm != nil {
			fmt.Println(&quot;error creating dir...&quot;)
			panic(errm)
		}
	}

	writeError := ioutil.WriteFile(filePath+&quot;/user.txt&quot;, []byte(`{&quot;user&quot;: &quot;Mac&quot;}`), 0644) // os.ModeTemporary)// 0644)
	if writeError == nil {
		fmt.Println(&quot;Ok write&quot;)
	} else {
		fmt.Println(&quot;Error write&quot;)
	}

	_, err := ioutil.ReadFile(filePath + &quot;/user.txt&quot;)
	if err == nil {
		fmt.Println(&quot;Reading file&quot;)
	} else {
		fmt.Println(&quot;Error reading file&quot;)
	}
}

It is like the os.Stat(filePath) thinks the folder is already there. If I then remove the check for os.Stat() and just go and create the folder I get a panic "not a directory
"?

fmt.Println("create: ", filePath) prints:

/private/var/folders/yj/jcyhsxxj6ml3tdfkp9gd2wpr0000gq/T/go-build627785093/command-line-arguments/_test/myio.test/json

It is all strange, as each test creates a new "/go-buildxxx/" folder and therefore the folder should never actually be there?

Any ideas?

答案1

得分: 2

首先,你需要从osext返回的基本路径中分离出可执行文件。

exePath, _ := osext.Executable()
base, _ := filepath.Split(exePath)

(或者使用osext.ExecutableFolder()

然后,你需要以权限0755创建目录。目录需要设置可执行位以便遍历它们。

英文:

First, you need to split off the executable from it's base path as it's returned from osext

exePath, _ := osext.Executable()
base, _ := filepath.Split(exePath)

(or use osext.ExecutableFolder())

Then you need to create the directory with the permissions 0755. Directories need the executable bit set in order to traverse them.

huangapple
  • 本文由 发表于 2014年12月12日 23:54:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/27447305.html
匿名

发表评论

匿名网友

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

确定