fengkuangrensheng
MrGao2018
采纳率0%
2016-03-05 12:25 阅读 4.4k

python中文件的readlines问题

name = input('please filename:')
infile = open(name,'rw+')
print('the filename is:',infile.name)
line = infile.readlines()
print('the data is:%s',line)
infile.close()

出现下面错误
please filename:readlines.txt
Traceback (most recent call last):
File "C:\Users\Administrator\workspace\text\readlines.py", line 2, in
infile = open(name,'rw+')
ValueError: must have exactly one of create/read/write/append mode

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

5条回答 默认 最新

  • bealing xSeeker~ 2016-03-05 12:37

    infile = open(name,'rw+')
    python 中文件打开操作的mode中没有“rw+”
    合法的mode有:
    r、rb、r+、rb+、w、wb、w+、wb+、a、ab、a+、ab+

    点赞 评论 复制链接分享
  • fengkuangrensheng MrGao2018 2016-03-05 12:40

    把rw+换成r的话还是有错
    Traceback (most recent call last):
    File "C:\Users\Administrator\workspace\text\readlines.py", line 2, in
    infile = open(name,'r')
    FileNotFoundError: [Errno 2] No such file or directory: 'readlines.txt'

    点赞 评论 复制链接分享
  • u013596119 u52983610 2016-03-05 12:47

    please filename:readlines.txt应该为please filename:"readlines.txt"

    点赞 评论 复制链接分享
  • fengkuangrensheng MrGao2018 2016-03-05 12:52

    为什么啊,有什么关系吗?

    点赞 评论 复制链接分享
  • oyljerry oyljerry 2016-03-05 15:20

    你文件路径对吗 检查一下

    点赞 评论 复制链接分享

相关推荐