免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 11646 | 回复: 11
打印 上一主题 下一主题

奇怪的os.listdir结果 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-08-06 20:33 |只看该作者 |倒序浏览
win XP环境,用python 2.7自带的IDLE,
当输入os.listdir("c:")时,结果是python安装目录
但是os.listdir("d:")却能得到d盘下的目录,os.listdir("c:\")也能得到c盘下的目录

大家有遇到这样的问题吗?

论坛徽章:
0
2 [报告]
发表于 2011-08-07 00:29 |只看该作者
回复 1# linux_arm

你试过 dir c: 和dir c:\ 有什么不同么?

论坛徽章:
0
3 [报告]
发表于 2011-08-07 10:50 |只看该作者
本帖最后由 nakar 于 2011-08-07 10:56 编辑

回复 2# pastebt


    真的很酷。

  1. D:\dev_py>dir c:tencent
  2. 驱动器 C 中的卷是 system
  3. 卷的序列号是 8CFF-C5EA

  4. C:\Program Files\tencent 的目录

  5. 2011-08-01  14:17    <DIR>          .
  6. 2011-08-01  14:17    <DIR>          ..
  7. 2011-08-01  14:17    <DIR>          QQ
  8. 2011-06-26  14:54    <DIR>          QQIntl
  9. 2011-08-01  14:17    <DIR>          QQSoftMgr
  10.                0 个文件              0 字节
  11.                5 个目录 16,137,007,104 可用字节
复制代码

论坛徽章:
0
4 [报告]
发表于 2011-08-08 22:01 |只看该作者
回复 2# pastebt


    没看懂你要说的啥意思,我试了两个,是有不同的,但不知道为什么

论坛徽章:
0
5 [报告]
发表于 2011-08-09 01:44 |只看该作者
回复 4# linux_arm


    这个不是python的问题,是windows或者说dos的问题,你可以看看 这里,希望能帮你理解这个问题
http://en.wikipedia.org/wiki/Current_working_directory

论坛徽章:
0
6 [报告]
发表于 2011-08-09 08:52 |只看该作者
回复 5# pastebt
http://en.wikipedia.org/wiki/Current_working_directory
pastebt 发表于 2011-08-09 01:44

这能解释lz的问题吗?

论坛徽章:
0
7 [报告]
发表于 2011-08-09 10:15 |只看该作者
本帖最后由 ccporxy 于 2011-08-09 10:22 编辑

源码跟进去发现 listdir 实际上是调用了win32 函数 FindFirstFile FindNextFile
来枚举目录信息。只不过会扩展一下传入的路径信息。

  1.             if (wch != L'/' && wch != L'\\' && wch != L':')
  2.                 wnamebuf[len++] = L'\\';
  3.             wcscpy(wnamebuf + len, L"*.*");
复制代码
当传入参数为'c系统所在分区),会扩展为'c:*.*',如果此时分区为系统分区的话(并且当前进程
所在目录也在同一个分区上),实际上查找的目录是当前进程所在目录。
其余情况<drive>: 等同于 <drive>:/ 等同于 <drive>:/*.* <drive>:/*
至于原因查看了msdn文档,没有收获。
还是请大神来解释吧。

查询了下关于path的描述:
Relative Paths
For functions that manipulate files, the file names can be relative to the current directory. A file name is relative to the current directory if it does not begin with one of the following:


A drive name, which is either a drive letter followed by a colon, or a UNC name.
A directory name separator (backslash), for example, \subdirectory).
If the file name begins with a disk designator and a backslash, it is a full path (for example, C:\tmp). If a file name begins with only a disk designator, it is a relative path to the current directory on the drive with the specified letter (for example, C:tmp.txt refers to a file in the current directory on drive C).

原因就在此了~

论坛徽章:
0
8 [报告]
发表于 2011-08-09 11:16 |只看该作者
回复 7# ccporxy
这个基本上差不多了。FindFirstFile 要求系统目录必须加\。
If you want to see files or get the attributes of a root directory, the following options would apply:

    To examine files in a root directory, you can use "C:\*" and step through the directory by using FindNextFile.
    To get the attributes of a root directory, use the GetFileAttributes function.

To examine a directory that is not a root directory, use the path to that directory, without a trailing backslash.

论坛徽章:
0
9 [报告]
发表于 2011-08-09 22:24 |只看该作者
感谢各位的答复~~受教了

论坛徽章:
0
10 [报告]
发表于 2011-08-10 09:54 |只看该作者
这个论坛果然牛人辈出啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP