鱼C论坛

 找回密码
 立即注册
查看: 1035|回复: 3

[已解决]为什么老是说我字符串阅读不了

[复制链接]
发表于 2019-8-1 15:47:54 | 显示全部楼层 |阅读模式

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

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

x
def sb(s1,n):
    f=open(s1)
    for i in range(n):
        print(s1.readline())
    s1.close()


s1=input('you want to open the file is :(please add txt and so on)')
n=int(input('input you want to show the line front:'))
sb(s1,n)



you want to open the file is :(please add txt and so on)C:/Users/ASUS/Desktop/sb3.txt
input you want to show the line front:2
Traceback (most recent call last):
  File "C:\Users\ASUS\Desktop\sb.py", line 10, in <module>
    sb(s1,n)
  File "C:\Users\ASUS\Desktop\sb.py", line 4, in sb
    print(s1.readline())
AttributeError: 'str' object has no attribute 'readline'
>>>
最佳答案
2019-8-1 16:05:17
  1. def sb(s1,n):
  2.      f=open(s1)
  3.      for i in range(n):
  4.          print(f.readline())
  5.      f.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-8-1 15:49:21 | 显示全部楼层
  1. def sb(s1,n):
  2.     f=open(s1)
  3.     for i in range(n):
  4.         print(f.readline())
  5.     f.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-1 16:02:39 | 显示全部楼层
本帖最后由 jackz007 于 2019-8-1 16:06 编辑

把这两句:
        print(s1.readline())
. . . . . .
    s1.close()

改成这样:
        print(f.readline())
. . . . . .
    f.close()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-1 16:05:17 | 显示全部楼层    本楼为最佳答案   
  1. def sb(s1,n):
  2.      f=open(s1)
  3.      for i in range(n):
  4.          print(f.readline())
  5.      f.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-6 09:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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