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'
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.stdoutwill 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With