推荐学习书目
› 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
noahs
V2EX  ›  Python

Python 报类型错误

  •  
  •   noahs · Dec 8, 2015 · 2727 views
    This topic created in 3963 days ago, the information mentioned may be changed or developed.

    小弟初学 python ,最新在练习做一个密码验证测试程序

    源码如下:

    !/usr/bin/python

    coding:utf-8

    username_f = open('username.txt')
    login_f = open('login.txt')

    for i in range(3):
    username = raw_input('username:').strip()
    password = raw_input('password:').strip()
    if len(username) != 0 and len(password) != 0:
    f = open(username_f)
    for line in f.readlines():
    if username == line.split()[0] and password == line.split()[1]:
    #判断输入的用户名与密码是否在 line 这个列别当中
    print "Welcome %s login my system " % username
    break
    break
    else:
    continue

    运行过程中输入完用户密码就报错如下:

    username:test
    password:test
    Traceback (most recent call last):
    File "C:\Users\lenvovx201i\Desktop\python\zuoye1.py", line 10, in <module>
    f = open(username_f)
    TypeError: coercing to Unicode: need string or buffer, file found

    请按任意键继续. . .


    各位老手请帮忙看看是什么问题

    3 replies  •  2015-12-08 17:17:05 +08:00
    yongzhong
        1
    yongzhong  
       Dec 8, 2015
    username_f = open('username.txt')已经打开一次了
    而在下面又
    f = open(username_f)
    问题很明显呀....
    noahs
        2
    noahs  
    OP
       Dec 8, 2015
    @yongzhong 谢谢,我在研究一下
    RickyBoy
        3
    RickyBoy  
       Dec 8, 2015
    f = open(open('username.txt'))
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3044 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 14:34 · PVG 22:34 · LAX 07:34 · JFK 10:34
    ♥ Do have faith in what you're doing.