ffts
V2EX  ›  Python

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

By ffts at 2014 年 2 月 25 日 · 6794 次点击
在windows下使用os.walk()浏览磁盘时,当浏览程序所在的磁盘时,会直接进入程序的目录
比如,我的程序在E:\code下,我用os.walk('E:')
结果当中dirpath是E:
但是dirnames,filenames变成了E:\code里的东西,而不是我想要的E:根目录的东西
但是浏览其他磁盘,比如C或D的时候就没有这个问题,会列出跟目录的文件夹和文件
然后我把代码拿到别的盘去执行的时候浏览E盘的时候就又好了,移过去的那个盘的根目录就又浏览不了了...
17 条回复  •  1970-01-01 08:00:00 +08:00
clino
   1
clino  
   2014 年 2 月 25 日   ❤️ 1
试试 os.walk('E:\\') 行不行?
ffts
   2
ffts  
OP
   2014 年 2 月 25 日
@clino
加了'\\'确实可以了,非常感谢!
话说为啥得加'\\',浏览其他的盘os.walk('C:')就没事,用os.path.join()拼出来的C:Windows什么的也可以浏览,很不解...
delo
   3
delo  
   2014 年 2 月 26 日 via iPhone   ❤️ 1
clino
   4
clino  
   2014 年 2 月 26 日   ❤️ 1
我想可以这么理解,C:是盘符,C:\才是路径,在python字符串里\是转义字符,所以要变成'\\'才行
jiangpeng
   5
jiangpeng  
   2014 年 2 月 26 日
有趣!PowerShell 里面 cd e: 就能切换盘符
oio
   6
oio  
   2014 年 2 月 26 日   ❤️ 1
与 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
   7
ffts  
OP
   2014 年 2 月 26 日
@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
   8
ffts  
OP
   2014 年 2 月 26 日
@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
   9
clino  
   2014 年 2 月 26 日
所以说windows的路径就是奇葩设计嘛,难于理解
C:tmp.txt这种路径有嘛用呀?
还是*nix的做法比较合理
clino
   10
clino  
   2014 年 2 月 26 日
还有这个\ ,c里面用来转义的字符也用在路径里,这不是sb嘛
ffts
   11
ffts  
OP
   2014 年 2 月 26 日
@clino 不晓得啊
yanze0613
   12
yanze0613  
   2014 年 2 月 27 日   ❤️ 1
@clino python在win下边,描述路径应该是都需要使用c:\\这种来描述,或者r'c:\'(后边这个没用过)
ffts
   13
ffts  
OP
   2014 年 2 月 27 日
@yanze0613 不过要是这样的话,其他的系统就识别不了了吧,一开始没用\\打算用os.path.join()来拼路径,想适应linux,windows还有mac
不过最后还是没有办法,对windonws单独处理了...反正windows也得列出磁盘,其他的倒不需要
yanze0613
   14
yanze0613  
   2014 年 2 月 27 日
系统分隔符可以使用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
   15
ffts  
OP
   2014 年 2 月 27 日
@yanze0613 原来还有这个可以用,不过这样一来确实在另外一个地方又要多加东西了...
另外,才知道原来mac os下分割符是':',我还以为也是'/'来着
oio
   16
oio  
   2014 年 2 月 27 日   ❤️ 1
磁盘符号 C:, D:, E:,只是命名空间,,不是文件或路径,这样理解就好了....

“\” 分隔符主要是历史原因了, “/” 在 DOS 已经有了别的意思,就选了 “\” 。
ffts
   17
ffts  
OP
   2014 年 2 月 27 日
@oio 唔,命名空间的话,确实就好理解一些了
推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
© 2026 V2EX · 29ms · 3.9.8.5