AttributeError:'元组'对象没有属性'写'

时间:2017-05-18 23:56:08

标签: python

line1 = (n1,'-',n2,'-',n3,'-',n4,'-',n5,'powerball =',pb,"----> Random       Numbers",'\n')
line2 = (l1,'-',l2,'-',l3,'-',l4,'-',l5,'powerball =',lpb,"----> Low Numbers",'\n')
line3 = (m1,'-',m2,'-',m3,'-',m4,'-',m5,'powerball =',mpb,"----> Medium Numbers",'\n')
line4 = (h1,'-',h2,'-',h3,'-',h4,'-',h5,'powerball =',hpb,"----> Hi Numbers",'\n')
line5 = (n1,'-',l2,'-',m3,'-',h4,'-',n5,'powerball =',lpb,"----> Mixed       Numbers",'\n')


file1 = (line1 + line2 + line3 + line4 + line5)

file_name = "file_name"
today1 = open(file_name , "r+")
file_name = file1
file_name.write();  

这个代码和我在写入时收到此错误 AttributeError:'元组'对象没有属性'写' 我读过我能找到的一切 我还是不知道自己做错了什么 我是Python的新手 感谢

1 个答案:

答案 0 :(得分:1)

没关系......我看到了问题:

file_name = "file_name"
today1 = open(file_name , "r+")
# At this point, you've opened the file "file_name"

file_name = file1
# Here, the variable file_name now contains that monster string
#   you put together.
# You have utterly lost the handle to your open file.

file_name.write();
# You have just tried to write output to that monster string.
# "write" is a file command, not a string operation.

也许你想要这样的东西:

file_name = "file_name"
today1 = open(file_name , "r+")
file_name.write(file1); 

这将(尝试)将该怪物字符串的值写入输出文件。我担心文件模式:“read extend”。您打算从文件中读取什么?你想用这个做什么?