推荐学习书目
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 · Apr 19, 2017 · 3413 views
    This topic created in 3332 days ago, the information mentioned may be changed or developed.

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

    12 replies    2017-04-19 15:49:54 +08:00
    mhycy
        1
    mhycy  
       Apr 19, 2017
    你的代码呢?
    raptium
        2
    raptium  
       Apr 19, 2017
    因为执行完一次以后,里层的迭代器已经读完了
    UnixCRoot
        3
    UnixCRoot  
    OP
       Apr 19, 2017
    @mhycy 嗯嗯?图不显示么?我再粘一下文本版的 稍等
    UnixCRoot
        4
    UnixCRoot  
    OP
       Apr 19, 2017
    @raptium 对 外层的只迭代一次 这是为何?
    raptium
        5
    raptium  
       Apr 19, 2017
    @UnixCRoot 外层迭代了很多次,但是从第二次开始,里层已经是空的了,所以看起来什么都没做
    UnixCRoot
        6
    UnixCRoot  
    OP
       Apr 19, 2017
    ```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  
       Apr 19, 2017
    break
    UnixCRoot
        8
    UnixCRoot  
    OP
       Apr 19, 2017
    @raptium 哦哦 谢谢 那么怎么才能让里层从头开始呢?
    UnixCRoot
        9
    UnixCRoot  
    OP
       Apr 19, 2017
    @gdsing 谢谢 不是 break 的事情 注释掉过了
    raptium
        10
    raptium  
       Apr 19, 2017
    @UnixCRoot open 写到里面
    UnixCRoot
        11
    UnixCRoot  
    OP
       Apr 19, 2017
    @raptium 感谢~懂了
    fds
        12
    fds  
       Apr 19, 2017
    PwdHandle.seek(0)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4444 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 64ms · UTC 05:32 · PVG 13:32 · LAX 22:32 · JFK 01:32
    ♥ Do have faith in what you're doing.