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
shyy228
V2EX  ›  Python

Python 报类型错误

  •  
  •   shyy228 · 2015-12-08 13:14:12 +08:00 · 2071 次点击
    这是一个创建于 3033 天前的主题,其中的信息可能已经有所发展或是发生改变。

    小弟初学 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 条回复    2015-12-08 17:17:05 +08:00
    yongzhong
        1
    yongzhong  
       2015-12-08 13:20:32 +08:00
    username_f = open('username.txt')已经打开一次了
    而在下面又
    f = open(username_f)
    问题很明显呀....
    shyy228
        2
    shyy228  
    OP
       2015-12-08 13:30:54 +08:00
    @yongzhong 谢谢,我在研究一下
    RickyBoy
        3
    RickyBoy  
       2015-12-08 17:17:05 +08:00
    f = open(open('username.txt'))
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2085 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 00:01 · PVG 08:01 · LAX 17:01 · JFK 20:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.