first use generators
List = open("/path/to/file")
List2 = (s.strip() + ' time' for s in List)
this leads to a lazy evaluation, so you don’t loop, don’t loop, and don’t loop, etc.
then correct your line ( , this is the actual problem causing the errors in your program )
for item in List2:
open('/home/user/Documents/%s.txt'%(item,), 'w')
so all your code will be
List = open("/path/to/file")
List2 = (s.strip() + ' time' for s in List)
for item in List2:
open('/home/user/Documents/%s.txt'%(item,), 'w')
now you only scroll through the list once instead of three times
the suggestion to use the filePath variable to form your file name is also very good
source
share