Reputation: 5469
My code is:
from random import randrange, choice
from string import ascii_lowercase as lc
from sys import maxsize
from time import ctime
tlds = ('com', 'edu', 'net', 'org', 'gov')
for i in range(randrange(5, 11)):
dtint = randrange(maxsize)
dtstr = ctime()
llen = randrange(4, 8)
login = ''.join(choice(lc)for j in range(llen))
dlen = randrange(llen, 13)
dom = ''.join(choice(lc) for j in range(dlen))
print('%s::%s@%s.%s::%d-%d-%d' % (dtstr, login,dom, choice(tlds),
dtint, llen, dlen), file='redata.txt')
I want to print the results in a text file but I get this error:
dtint, llen, dlen), file='redata.txt')
AttributeError: 'str' object has no attribute 'write'
Upvotes: 2
Views: 713
Reputation: 65791
file
should be a file object, not a file name. File objects have write
method, str
objects don't.
From the doc on print
:
The file argument must be an object with a
write(string)
method; if it is not present orNone
,sys.stdout
will be used.
Also note that the file should be open for writing:
with open('redata.txt', 'w') as redata: # note that it will overwrite old content
for i in range(randrange(5,11)):
...
print('...', file=redata)
See more about the open
function here.
Upvotes: 9