英文:
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 (
"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")
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论