brilliant_moves

EveryThirdLine.py

Jan 19th, 2015
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.43 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. def extract_third_lines():
  4.     try:
  5.         filename = raw_input("Enter name of file to read: ")
  6.         with open(filename) as f:
  7.             lines = f.readlines()
  8.             my_str = ""
  9.             for line in lines[2::3]:
  10.                 my_str += line
  11.    
  12.         return my_str # extracts string made from every 3rd line
  13.     except IOError:
  14.         return "Error: can't find file or read data."
  15.     else:
  16.         f.close()
  17.        
  18. def main():
  19.     print extract_third_lines()
  20.    
  21. main()
Advertisement
Add Comment
Please, Sign In to add comment