V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
UnixCRoot
V2EX  ›  Python

Python 用迭代器嵌套迭代两个文件,最外层迭代只执行一次,里层正常

  •  
  •   UnixCRoot ·
    BackTrackCRoot · 2017-04-19 09:44:49 +08:00 · 2370 次点击
    这是一个创建于 2557 天前的主题,其中的信息可能已经有所发展或是发生改变。

    写一个简单的密码测试工具,一个 user 文件,一个 pwd 文件,然后迭代 user 里面嵌套迭代 pwd ,结果 user 只迭代了一行, pwd 迭代正常,这是为啥? 代码图片 就是 user 那个只迭代了一次就完事了,字典文件都是多行的。 求指点一下 是不是指针问题??

    12 条回复    2017-04-19 15:49:54 +08:00
    mhycy
        1
    mhycy  
       2017-04-19 09:46:10 +08:00
    你的代码呢?
    raptium
        2
    raptium  
       2017-04-19 09:49:18 +08:00
    因为执行完一次以后,里层的迭代器已经读完了
    UnixCRoot
        3
    UnixCRoot  
    OP
       2017-04-19 09:49:23 +08:00
    @mhycy 嗯嗯?图不显示么?我再粘一下文本版的 稍等
    UnixCRoot
        4
    UnixCRoot  
    OP
       2017-04-19 09:49:44 +08:00
    @raptium 对 外层的只迭代一次 这是为何?
    raptium
        5
    raptium  
       2017-04-19 09:51:07 +08:00
    @UnixCRoot 外层迭代了很多次,但是从第二次开始,里层已经是空的了,所以看起来什么都没做
    UnixCRoot
        6
    UnixCRoot  
    OP
       2017-04-19 09:51:22 +08:00
    ```python
    def DictAttck(Host,UsernameFile,PasswordFile):
    UserHandle = open(UsernameFile)
    PwdHandle = open(PasswordFile)
    for user in UserHandle:
    for pwd in PwdHandle:
    print Fore.RED + "[***] " + Style.RESET_ALL + "Try to UserName:%s Password:%s"%(user,pwd)
    if chekpassword(Host,user,pwd) == 1:
    print Fore.GREEN + "[OK] " + Style.RESET_ALL + "Got password. Username:%s Password:%s" %(user,pwd)
    break
    ```
    gdsing
        7
    gdsing  
       2017-04-19 09:52:29 +08:00
    break
    UnixCRoot
        8
    UnixCRoot  
    OP
       2017-04-19 09:52:36 +08:00
    @raptium 哦哦 谢谢 那么怎么才能让里层从头开始呢?
    UnixCRoot
        9
    UnixCRoot  
    OP
       2017-04-19 09:53:08 +08:00
    @gdsing 谢谢 不是 break 的事情 注释掉过了
    raptium
        10
    raptium  
       2017-04-19 09:53:27 +08:00
    @UnixCRoot open 写到里面
    UnixCRoot
        11
    UnixCRoot  
    OP
       2017-04-19 09:54:33 +08:00
    @raptium 感谢~懂了
    fds
        12
    fds  
       2017-04-19 15:49:54 +08:00
    PwdHandle.seek(0)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2714 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:53 · PVG 22:53 · LAX 07:53 · JFK 10:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.