By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,737 Members | 1,321 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,737 IT Pros & Developers. It's quick & easy.

open file and iterate+print to file

P: 2
I am attempting to open a file and write to that file while iterating through another file.

The result in the file I am attempting to write to should
be

hostname1 192.168.100.11
hostname2 192.168.100.12



Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. # http://docs.python.org/2/library/pprint.html
  3. #The output written to a file is python dictionary format
  4. # http://www.tutorialspoint.com/python/python_dictionary.htm
  5.  
  6. import yaml
  7. import pprint
  8.  
  9.  
  10. f = open('/etc/puppet/hieradata/l0015.yaml')
  11. # use safe_load instead load
  12. dataMap = yaml.safe_load(f)
  13. f.close()
  14.  
  15. d = dataMap
  16. for key, value in d.iteritems():
  17.  print key,  value
  18.  
  19. d = open('/etc/puppet/hieradata/0015idump.yaml', "w")
  20. z = dataMap
  21. for key, value in z.iteritems():
  22.  print key,  value,
  23. d.close()
Jun 5 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.