第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法使用 golang 刪除解壓縮的文件夾

無法使用 golang 刪除解壓縮的文件夾

Go
蝴蝶刀刀 2021-12-07 10:26:27
我編寫了在特定位置解壓縮文件的代碼,然后將文件夾的內(nèi)容復(fù)制到解壓縮文件夾的外部,然后刪除該文件夾。問題是除了刪除文件夾外,一切正常。該文件夾中只有一個文件。文件位置如下:E:\go\copyDirectory\myfile\mytextfile.txtzip文件的位置如下: E:\go\copyDirectory\myfile.zipzip 文件只有一個文本文件。zip文件里面的File如下:E:\go\copyDirectory\myfile.zip\myfile\mytextfile.txt我得到的錯誤是:ERRR::: remove myfile\mytextfile.txt: The process cannotaccess the file because it is being used by another process.提前致謝。
查看完整描述

1 回答

?
繁星淼淼

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個贊

你沒有關(guān)閉文件。這個:


defer newTempFileHandle.Close()

在 main 完成時運(yùn)行,這是在:


err = RemoveContents("./myFiles")

您可以將那段代碼包裝在一個未命名的函數(shù)中:


    func() {

        //read the file or folder handle inside zip

        fileOpenHandle, err := fileReadHandler.Open()

        if err != nil {

            fmt.Println(err)

            os.Exit(1)

        }

        defer fileOpenHandle.Close()

        targetUnZipPath := filepath.Join(tempWrkDir, fileReadHandler.Name)

        if fileReadHandler.FileInfo().IsDir() {

            os.MkdirAll(targetUnZipPath, fileReadHandler.Mode())

            //fmt.Println("Creating directory", path)

        } else {

            // create new dummy file to copy original file.

            newTempFileHandle, err := os.OpenFile(targetUnZipPath, os.O_WRONLY|os.O_CREATE, fileReadHandler.Mode())


            if err != nil {

                fmt.Println(err)

                os.Exit(1)

            }


            defer newTempFileHandle.Close()

            //copying original file to dummy file.

            if _, err = io.Copy(newTempFileHandle, fileOpenHandle); err != nil {

                fmt.Println(err)

                os.Exit(1)

            }

        }

    }()

然后您的延遲將在您嘗試刪除文件之前發(fā)生。不過,我建議將其提取到命名函數(shù)中。


查看完整回答
反對 回復(fù) 2021-12-07
  • 1 回答
  • 0 關(guān)注
  • 288 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號