1

This is what I have right now:

def open_file():
    file_name = input("What is the name of the file you want to open?")
    while True:
        try:
            file = open(file_name, 'r')
            header = file.readline()
            return (file)
            break
        except FileNotFoundError:
            file_name = input("What is the name of the file you want to open?")

def process_file():
    file = open_file()
    print(file)

def main():
    process_file()

I can't even get it to get to prompting me for the file name I want to open. Doesn't that mean it's not a problem with my loop but the way I am calling my functions?

Mark Skelton
  • 3,663
  • 4
  • 27
  • 47
johnson
  • 121
  • 5

3 Answers3

3

Assuming you called your module something like foo.py

You could add this bit of code:

if __name__ == '__main__':
    main()

And then in the console run:

  >>python foo.py

For an explanation of why this works, you can see this answer: What does if __name__ == "__main__": do?

Community
  • 1
  • 1
Juan
  • 915
  • 1
  • 9
  • 13
1

You need to call main() to run your code. If you do that it works just fine.

Mark Skelton
  • 3,663
  • 4
  • 27
  • 47
1

You need to call main() in order for your code to run.

Leo
  • 472
  • 5
  • 9