鱼C论坛

 找回密码
 立即注册
查看: 5970|回复: 4

030-os.walk( ) 为什么没有返回一个三元组呢?

[复制链接]
发表于 2016-4-16 19:55:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在030课后题 - 课后题 4 中 ,小甲鱼使用了 os.walk(top) 语句
但是、当我写了一个程序、单独运行 os.walk(top) 时、并没有返回一个三元组,请看程序

程序
  1. import os
  2. s=os.walk(os.curdir)
  3. print(s)
复制代码


运行结果
  1. >>>
  2. <generator object walk at 0x02A07D28>
复制代码


问题:
1.为什么没有返回一个三元组呢?

2.该如何正确的使用 os.walk(top) 这条语句?

希望大神能解答一下~


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-4-16 21:52:21 | 显示全部楼层
  1. >>> import os
  2. >>> print(os.curdir)
  3. .
  4. >>> os.chdir('E:')
  5. >>> s = os.walk(os.curdir)
  6. >>> s.next()
  7. Traceback (most recent call last):
  8.   File "<pyshell#4>", line 1, in <module>
  9.     s.next()
  10. AttributeError: 'generator' object has no attribute 'next'
  11. >>> s[1]
  12. Traceback (most recent call last):
  13.   File "<pyshell#5>", line 1, in <module>
  14.     s[1]
  15. TypeError: 'generator' object is not subscriptable
  16. >>> for root, dirs, files in os.walk(os.curdir):
  17.         print('{0}\n{1}\n{2}'.format(root, dirs, files))

  18.        
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-4-16 21:57:01 | 显示全部楼层
或者这样!
  1. >>> import os
  2. >>> s = os.walk(os.curdir)
  3. >>> print(s)
  4. <generator object walk at 0x00D2E558>
  5. >>> s
  6. <generator object walk at 0x00D2E558>
  7. >>> help(s)
  8. Help on generator object:

  9. walk = class generator(object)
  10. |  Methods defined here:
  11. |  
  12. |  __del__(...)
  13. |  
  14. |  __getattribute__(self, name, /)
  15. |      Return getattr(self, name).
  16. |  
  17. |  __iter__(self, /)
  18. |      Implement iter(self).
  19. |  
  20. |  __next__(self, /)
  21. |      Implement next(self).
  22. |  
  23. |  __repr__(self, /)
  24. |      Return repr(self).
  25. |  
  26. |  close(...)
  27. |      close() -> raise GeneratorExit inside generator.
  28. |  
  29. |  send(...)
  30. |      send(arg) -> send 'arg' into generator,
  31. |      return next yielded value or raise StopIteration.
  32. |  
  33. |  throw(...)
  34. |      throw(typ[,val[,tb]]) -> raise exception in generator,
  35. |      return next yielded value or raise StopIteration.
  36. |  
  37. |  ----------------------------------------------------------------------
  38. |  Data descriptors defined here:
  39. |  
  40. |  gi_code
  41. |  
  42. |  gi_frame
  43. |  
  44. |  gi_running

  45. >>> s.__next__()
  46. ('.', ['DLLs', 'Doc', 'include', 'Lib', 'libs', 'Scripts', 'tcl', 'Tools'], ['first.py', 'jchess.py', 'LICENSE.txt', 'NEWS.txt', 'opt_parse.py', 'python.exe', 'pythonw.exe', 'qt.conf', 'README.txt', 'test.txt', 'tmp.py'])
  47. >>> s.__next__()
  48. ('.\\DLLs', [], ['py.ico', 'pyc.ico', 'pyexpat.pyd', 'python3.dll', 'select.pyd', 'sqlite3.dll', 'tcl86t.dll', 'tk86t.dll', 'unicodedata.pyd', 'winsound.pyd', '_bz2.pyd', '_ctypes.pyd', '_ctypes_test.pyd', '_decimal.pyd', '_elementtree.pyd', '_hashlib.pyd', '_lzma.pyd', '_msi.pyd', '_multiprocessing.pyd', '_overlapped.pyd', '_socket.pyd', '_sqlite3.pyd', '_ssl.pyd', '_testbuffer.pyd', '_testcapi.pyd', '_testimportmultiple.pyd', '_tkinter.pyd'])
  49. >>> s.__next__()
  50. ('.\\Doc', [], ['python340.chm'])
  51. >>> s.__next__()
  52. ('.\\include', [], ['abstract.h', 'accu.h', 'asdl.h', 'ast.h', 'bitset.h', 'bltinmodule.h', 'boolobject.h', 'bytearrayobject.h', 'bytesobject.h', 'bytes_methods.h', 'cellobject.h', 'ceval.h', 'classobject.h', 'code.h', 'codecs.h', 'compile.h', 'complexobject.h', 'datetime.h', 'descrobject.h', 'dictobject.h', 'dtoa.h', 'dynamic_annotations.h', 'enumobject.h', 'errcode.h', 'eval.h', 'fileobject.h', 'fileutils.h', 'floatobject.h', 'frameobject.h', 'funcobject.h', 'genobject.h', 'graminit.h', 'grammar.h', 'import.h', 'intrcheck.h', 'iterobject.h', 'listobject.h', 'longintrepr.h', 'longobject.h', 'marshal.h', 'memoryobject.h', 'metagrammar.h', 'methodobject.h', 'modsupport.h', 'moduleobject.h', 'namespaceobject.h', 'node.h', 'object.h', 'objimpl.h', 'opcode.h', 'osdefs.h', 'parsetok.h', 'patchlevel.h', 'pgen.h', 'pgenheaders.h', 'pyarena.h', 'pyatomic.h', 'pycapsule.h', 'pyconfig.h', 'pyctype.h', 'pydebug.h', 'pyerrors.h', 'pyexpat.h', 'pyfpe.h', 'pygetopt.h', 'pyhash.h', 'pymacconfig.h', 'pymacro.h', 'pymath.h', 'pymem.h', 'pyport.h', 'pystate.h', 'pystrcmp.h', 'pystrtod.h', 'Python-ast.h', 'Python.h', 'pythonrun.h', 'pythread.h', 'pytime.h', 'py_curses.h', 'rangeobject.h', 'setobject.h', 'sliceobject.h', 'structmember.h', 'structseq.h', 'symtable.h', 'sysmodule.h', 'token.h', 'traceback.h', 'tupleobject.h', 'typeslots.h', 'ucnhash.h', 'unicodeobject.h', 'warnings.h', 'weakrefobject.h'])
  53. >>>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-17 22:01:22 | 显示全部楼层

我试了一下,如果真的要把这些打印出来的话,会占用很多的空间,所以还是直接调用好了。

  1. import os
  2. s=os.walk(os.curdir)
  3. for root, dirs, files in s:
  4.     for i in files:
  5.         if('.txt' in i):  # 判断是否是txt文件
  6.             print(i)  #打印该文件的名字
复制代码


大神、平常这样用可以么~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-4-17 22:20:47 | 显示全部楼层
你看的到,s是生成器。
import os
s=os.walk(os.curdir)
for i in s:
    print(i)


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 23:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表