os.RemoveAll doesn't seem to work.

blov · · 1602 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>All, I&#39;m trying to implement a process to remove all directories named &#34;bin&#34; and &#34;obj&#34; in a directory tree. I&#39;ve copied my code to the playground, obviously it won&#39;t run there.</p> <p><a href="http://play.golang.org/p/SeutuUbNHo" rel="nofollow">http://play.golang.org/p/SeutuUbNHo</a></p> <p>In my case I get no errors when I call os.RemoveAll, but every indication I have says it should be working. Can anyone review my code for an obvious flaw or test os.RemoveAll and confirm it doesn&#39;t work on OSX?</p> <hr/>**评论:**<br/><br/>Justinsaccount: <pre><p>Code looks wrong to me.. You are walking root, but joining with the current directory to get the full path. Why the discrepancy?</p></pre>caseynashvegas: <pre><p>Yes, but root in my test was &#34;.&#34; which made it the same as my working directory. I added the os.Getwd call and path.Join only as part of troubleshooting the os.RemoveAll call. I wasn&#39;t thinking about the fact the 2 would not always match because they would in my test case. </p> <p>Good observation though!</p></pre>pyThat: <pre><p>I believe <code>RemoveAll</code> doesn&#39;t check if the directory exist or not, it will just tell you that it deleted it.</p> <p>Your code will work if you pass to it relative paths, because you are getting the current working directory and appending that to whatever <code>Arg(0)</code> has, so if your current working directory is <code>/Users/reddit/Desktop</code> and you pass <code>/Users/reddit/messy</code> it will try to cleanup <code>/Users/reddit/Desktop/Users/reddit/messy</code>, but if you&#39;ll pass the directory relative to your current working directory so <code>Arg(0)</code> will be <code>../messy</code> it will work.</p> <p>PS: pay attention to your <code>fmt.Println</code> messages</p></pre>caseynashvegas: <pre><p>As I explained before it was the same path....I should have removed that before posting, just forgot. I originally was testing with just the relative path.</p></pre>pyThat: <pre><p>I tested your code without editing anything and it works just fine with relative paths.</p> <p>I&#39;m using OS X 10.10.5, GO 1.5</p></pre>caseynashvegas: <pre><p>Thanks!</p> <p>So my guess there is probably an error not being reported preventing the delete.</p></pre>caseynashvegas: <pre><p>Now I totally can&#39;t reproduce this. I am guessing it was something environmental that prevented the delete, but for some reason it wasn&#39;t raised as an error. Ohh, well. It works on my machine :)</p></pre>i_regret_most_of_it: <pre><blockquote> <p>If the path does not exist, RemoveAll returns nil (no error). </p> </blockquote> <p>So, when &#39;Deleted: ...&#39; is printed, if you <code>ls</code> that exact path immediately after the program runs, the directory definitely exists?</p></pre>caseynashvegas: <pre><p>Yes, after execution with no errors, the directories definitely still exist. The only possibly weird thing I&#39;m doing is that I&#39;m simply executing with go run instead of compiling and then executing. I have several things like this I just treat as scripts.</p></pre>JHunz: <pre><p>Are they marked read-only? I know the golang community has gone back and forth quite a bit on whether RemoveAll should remove read-only files and directories or not, but I believe it&#39;s still settled on the original behavior - that it won&#39;t.</p></pre>caseynashvegas: <pre><p>No, not read only.</p></pre>gohacker: <pre><p>Go version?</p></pre>caseynashvegas: <pre><p>1.5</p></pre>gohacker: <pre><p>It should work, so this is a bug. Open an issue: <a href="https://github.com/golang/go/issues" rel="nofollow">https://github.com/golang/go/issues</a></p></pre>caseynashvegas: <pre><p>And I&#39;m downvoted. I&#39;ll never pretend to understand reddit.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1602 次点击  
加入收藏 微博
0 回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传