Golang无法写入文本文件:句柄无效。

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

golang can't write to text file: the handle is invalid

问题

大家好,

我很抱歉这么快又提出一个问题,但最近在使用Go语言时总是一个接一个的问题。

我有一个工作中的网页爬虫(多亏了大家的帮助),可以从这个维基页面上获取我想要的所有信息:http://monsterhunter.wikia.com/wiki/MH4U:_Item_List

它可以显示我想要的所有内容,没有问题。然而,当我尝试写入一个.txt文件时,出现了一个错误,错误信息是:"0 write mh4u.txt: The handle is invalid"。

以下是我目前的代码供参考:

package main

import (
	"fmt"
	"log"

	"github.com/PuerkitoBio/goquery"
	"os"
	"io"
)

func main() {

	filename := "mh4u.txt"
	file, err := os.Create(filename)
	
	if err!= nil {
		fmt.Println(err)
	}
	doc, err := goquery.NewDocument("http://www.ign.com/wikis/monster-hunter-4/Items")
	if err != nil {
		log.Fatal(err)
	}

	doc.Find("tbody").Each(func(i int, s *goquery.Selection) {

		s.Find("td").Each(func(j int, s2 *goquery.Selection) {
			
			if s3 := s2.Find("img"); s3 != nil && s3.Length() > 0 {
				return
			}

			fmt.Printf(s2.Text())
			n, err := io.WriteString(file, s2.Text())
			
			if err != nil {
				fmt.Println(n, err) 
			}
		})

		file.Close()
	})
}

通过测试其他网站,我认为可能有一些隐藏字符导致写入操作出现问题,但这只是我猜测的原因。

非常感谢您提供的任何建议/提示/解决方案!

英文:

Hello again everybody,

I apologize for asking another question so soon, but it just seems to be one thing after another with Go lately.

I have a working web page scraper (thanks to everybody's help) that grabs all the info I want from this wiki page: http://monsterhunter.wikia.com/wiki/MH4U:_Item_List

It then displays everything I want, no hiccups. However, when I go to write to a .txt file I get an error stating: "0 write mh4u.txt: The handle is invalid"

Here is my current code for reference:

package main

import (
	"fmt"
	"log"

	"github.com/PuerkitoBio/goquery"
	"os"
	"io"
)

func main() {

	filename := "mh4u.txt"
	file, err := os.Create(filename)
	
	if err!= nil {
		fmt.Println(err)
	}
	doc, err := goquery.NewDocument("http://www.ign.com/wikis/monster-hunter-4/Items")
	if err != nil {
		log.Fatal(err)
	}

	doc.Find("tbody").Each(func(i int, s *goquery.Selection) {

		s.Find("td").Each(func(j int, s2 *goquery.Selection) {
			
			if s3 := s2.Find("img"); s3 != nil && s3.Length() > 0 {
				return
			}

			fmt.Printf(s2.Text())
			n, err := io.WriteString(file, s2.Text())
			
			if err != nil {
				fmt.Println(n, err) 
			}
		})

		file.Close()
	})
}

Testing this code with other web sites leads me to believe that there is maybe some hidden characters that is giving the writer some issues, but that's the only thing I can guess.

Thanks so much for any suggestions/tips/solutions you can offer!

答案1

得分: 6

你正在闭包内调用file.Close()。如果doc.Find("tbody").Each被多次调用,你将尝试向已关闭的文件写入。你应该在创建文件后立即延迟关闭文件:

file, err := os.Create(filename)
if err != nil {
    log.Fatal(err)
}
defer file.Close()
英文:

You're calling file.Close() inside a closure. If doc.Find("tbody").Each is called more than one time, you'll end up trying to write to a closed file. You should defer the file closing right after you've created it:

file, err := os.Create(filename)
if err != nil {
    log.Fatal(err)
}
defer file.Close()

huangapple
  • 本文由 发表于 2015年6月2日 17:32:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/30592307.html
匿名

发表评论

匿名网友

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

确定