0

I am having trouble reading in files through Python 2.7. I prompt the user to load a file, and once the filename is given, the program will simply load and print the lines in the file.

filename = raw_input("Filename to load: ")
print load_records(students, filename)

def load_records(students, filename):
    #loads student records from a file
    records = []
    in_file = open(filename, "r")
    for line in in_file:
        print line

However, even if the entire file path is specified, the program throws a 'ValueError: mixing iterations and read methods would lose data.'

SethMMorton
  • 45,752
  • 12
  • 65
  • 86
errolflynn
  • 641
  • 2
  • 11
  • 24
  • Please make sure to use the code formatting options (4 space indent on each code line). – Clint Powell Feb 06 '14 at 03:32
  • 1
    The code works for me. Well, after moving `load_records` definition before `print load_records(..)` line and defining what `students` is. Python 2.7.6 – zaquest Feb 06 '14 at 03:43
  • Can you post the complete traceback? Just "ValueError: mixing iterations and read methods would lose data." is nice, but the full traceback actually shows what line the error is on, where on that line. If you don't know what a traceback is, please ask. – SethMMorton Feb 06 '14 at 03:50
  • possible duplicate of [Python: Mixing files and loops](http://stackoverflow.com/questions/826493/python-mixing-files-and-loops) – SethMMorton Feb 06 '14 at 03:56
  • If you look at the linked question, you will see that you are probably doing some other things that you haven't put in the question. Fix those things and you should get it to work. And when you post question, include everything that is needed to replicate the problem. – SethMMorton Feb 06 '14 at 03:58

0 Answers0