Chinaunix

标题: sort() 报错 [打印本页]

作者: luwenju    时间: 2012-11-23 16:05
标题: sort() 报错
使用sort()时报错,脚本如下
[root@localhost py]# cat a.py        
#!/usr/bin/python
from sys import argv
script, filename = argv

tmpfile = open(filename)
file = tmpfile.read()

print file.sort()

[root@localhost py]#
[root@localhost py]#
[root@localhost py]# ./a.py a.txt     //报错信息如下
Traceback (most recent call last):
  File "./a.py", line 8, in ?
    print file.sort()
AttributeError: 'str' object has no attribute 'sort'




请问是什么原因造成的?





作者: mstools    时间: 2012-11-23 16:33
很明显的错误呀,  str 没有 sort 方法,,
文件的read方法返回的是str,
readlines方法才返回的是list,
改成readlines就可以了.
作者: luwenju    时间: 2012-11-23 17:01
回复 2# mstools


    已解决,ths   







欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2