python 里 os.walk 在 windows 下浏览磁盘根目录的问题

2014-02-25 22:45:26 +08:00
 ffts
在windows下使用os.walk()浏览磁盘时,当浏览程序所在的磁盘时,会直接进入程序的目录
比如,我的程序在E:\code下,我用os.walk('E:')
结果当中dirpath是E:
但是dirnames,filenames变成了E:\code里的东西,而不是我想要的E:根目录的东西
但是浏览其他磁盘,比如C或D的时候就没有这个问题,会列出跟目录的文件夹和文件
然后我把代码拿到别的盘去执行的时候浏览E盘的时候就又好了,移过去的那个盘的根目录就又浏览不了了...
5295 次点击
所在节点    Python
17 条回复
clino
2014-02-25 22:57:42 +08:00
试试 os.walk('E:\\') 行不行?
ffts
2014-02-25 23:22:27 +08:00
@clino
加了'\\'确实可以了,非常感谢!
话说为啥得加'\\',浏览其他的盘os.walk('C:')就没事,用os.path.join()拼出来的C:Windows什么的也可以浏览,很不解...
delo
2014-02-26 00:44:32 +08:00
clino
2014-02-26 09:01:22 +08:00
我想可以这么理解,C:是盘符,C:\才是路径,在python字符串里\是转义字符,所以要变成'\\'才行
jiangpeng
2014-02-26 09:31:08 +08:00
有趣!PowerShell 里面 cd e: 就能切换盘符
oio
2014-02-26 10:52:22 +08:00
与 Python 无关,“E:” 不是合法路径,必须要有“/” 或者“\”。

If a file name begins with only a disk designator but not the backslash after the colon, it is interpreted as a relative path to the current directory on the drive with the specified letter.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
ffts
2014-02-26 13:02:38 +08:00
@clino
@delo

@oio de 链接里找到了
"C:tmp.txt" refers to a file named "tmp.txt" in the current directory on drive C.
"C:tempdir\tmp.txt" refers to a file in a subdirectory to the current directory on drive C.

有点理解,又有点不理解...
就是说,我程序运行在E:\code\下的时候,current directory默认是E:\code,这时候要是用E:的话,实际上就是E:\code?
然后,用其他的比如C: D: 的话,因为current directory在这些盘符上没有,所以就直接列出了相应盘符的根目录?
不知道是不是可以这样理解
ffts
2014-02-26 13:38:14 +08:00
@oio

啊,这句话一开始没看太懂,现在好像明白了...
If a file name begins with only a disk designator but not the backslash after the colon, it is interpreted as a relative path to the current directory on the drive with the specified letter.
应该是会跑到当前目录里...
clino
2014-02-26 21:11:29 +08:00
所以说windows的路径就是奇葩设计嘛,难于理解
C:tmp.txt这种路径有嘛用呀?
还是*nix的做法比较合理
clino
2014-02-26 21:12:55 +08:00
还有这个\ ,c里面用来转义的字符也用在路径里,这不是sb嘛
ffts
2014-02-26 21:16:15 +08:00
@clino 不晓得啊
yanze0613
2014-02-27 09:38:55 +08:00
@clino python在win下边,描述路径应该是都需要使用c:\\这种来描述,或者r'c:\'(后边这个没用过)
ffts
2014-02-27 10:11:07 +08:00
@yanze0613 不过要是这样的话,其他的系统就识别不了了吧,一开始没用\\打算用os.path.join()来拼路径,想适应linux,windows还有mac
不过最后还是没有办法,对windonws单独处理了...反正windows也得列出磁盘,其他的倒不需要
yanze0613
2014-02-27 11:12:52 +08:00
系统分隔符可以使用os.sep,问题是,麻烦的很是每一个连接符都要用
Notice the use of the os.sep variable - this gives the directory separator according
to your operating system i.e. it will be ’/’ in GNU/Linux and Unix, it will be
’\\’ in Windows and ’:’ in Mac OS. Using os.sep instead of these characters
directly will make our program portable and work across all of these systems
某pdf的介绍
@ffts
ffts
2014-02-27 11:55:59 +08:00
@yanze0613 原来还有这个可以用,不过这样一来确实在另外一个地方又要多加东西了...
另外,才知道原来mac os下分割符是':',我还以为也是'/'来着
oio
2014-02-27 12:31:46 +08:00
磁盘符号 C:, D:, E:,只是命名空间,,不是文件或路径,这样理解就好了....

“\” 分隔符主要是历史原因了, “/” 在 DOS 已经有了别的意思,就选了 “\” 。
ffts
2014-02-27 13:16:28 +08:00
@oio 唔,命名空间的话,确实就好理解一些了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/101871

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX