From f2d1e7329b48570afce7a0a8309abac1ecd14f88 Mon Sep 17 00:00:00 2001 From: baoxuebin Date: Sat, 22 Oct 2022 23:34:28 +0800 Subject: [PATCH] fix: account balance failed when file is not exist. --- script/file.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/script/file.go b/script/file.go index e6ff8ad..ac796de 100644 --- a/script/file.go +++ b/script/file.go @@ -38,6 +38,10 @@ func WriteFile(filePath string, content string) error { } func AppendFileInNewLine(filePath string, content string) error { + err := CreateFileIfNotExist(filePath) + if err != nil { + return err + } content = "\r\n" + content file, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, os.ModeAppend) if err != nil { @@ -71,6 +75,20 @@ func CreateFile(filePath string) error { return nil } +func CreateFileIfNotExist(filePath string) error { + if _, e := os.Stat(filePath); os.IsNotExist(e) { + _ = os.MkdirAll(filepath.Dir(filePath), os.ModePerm) + f, err := os.Create(filePath) + if nil != err { + LogSystemError(filePath + " create failed") + return err + } + defer f.Close() + LogSystemInfo("Success create file " + filePath) + } + return nil +} + func CopyFile(sourceFilePath string, targetFilePath string) error { if !FileIfExist(sourceFilePath) { panic("File is not found, " + sourceFilePath)