os.Remove not working in windows

1,286 views
Skip to first unread message

Shenal Silva

unread,
Aug 28, 2014, 5:52:45 AM8/28/14
to golang-nuts
Hi All,

The following code works fine in Linux  but when I compile and run in Windows the os.Remove function does not work or throw any errors. What can be the reason.??

infile := dl.fileName + "." + strconv.Itoa(j)
in, _ := os.Open(infile)
defer os.Remove(infile)

_, err := io.Copy(out, in)

if err != nil {
fmt.Fprintln("Error occured", err))
                         err:=os.Remove(infile)
if err!=nil {
fmt.Fprintln(err)

} else {
fmt.Fprintln("file ", infile, "joined")
err:=os.Remove(infile)
if err!=nil {
fmt.Fprintln(err)
}


Regards,
Shenal Silva,
          +94786903735
Skype:shenal777

Dmitry Vyukov

unread,
Aug 28, 2014, 6:08:51 AM8/28/14
to Shenal Silva, golang-nuts
windows does not allow to delete opened files or dirs with opened files
but you can rename it
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Francisco Dalla Rosa Soares

unread,
Aug 28, 2014, 6:09:15 AM8/28/14
to Shenal Silva, golang-nuts
I might be wrong but I think it's because you're not closing the file before removing it. On Linux you can remove a file even if it's in use but on windows you can't so, try to close&remove instead of just removing the file

2014年8月28日木曜日、Shenal Silva<shenal....@gmail.com>さんは書きました:
--

Shenal Silva

unread,
Aug 28, 2014, 6:18:38 AM8/28/14
to Francisco Dalla Rosa Soares, golang-nuts
Thanx that fixed the issue

Regards,
Shenal Silva,
          +94786903735
Skype:shenal777


Reply all
Reply to author
Forward
0 new messages