I\'m new with Python and I know that piece of code is very simple and lacks some statements, actually I need to write to file from dictionary. This code runs but only writes
If you want to keep adding lines to your file, open it with the a mode, as described in the documentation:
for (name, mobile) in ab.iteritems():
with open(...., "a") as f:
print ('Contact %s at %s' % (name, mobile))
f.write(name)
f.write(mobile)
Using w as mode means writing: your file will be overwritten.
Each time you open the file in w mode, its previous content is erased. So you only should do it once, before the loop. Best of all, do it with a with statement:
ab={'engy':'011199887765',
'wafa2':'87878857578',
'heba':'6677553636'}
with open('D:\glo.txt','w') as f:
for name, mobile in ab.items():
print ('Contact %s at %s' % (name, mobile))
f.write(lis)
f.write(mobile)
Also, I don't know what lis is, but I'll assume it's in the right place. Note that your code only writes lis numbers to the file, not names. lis doesn't change in the loop, so it'll be the same on each iteration.