2

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,9214 gold badges33 silver badges51 bronze badges
asked Feb 29, 2016 at 20:26
1
  • 2
    Unlike C, main() isn't called automatically. Commented Feb 29, 2016 at 20:28

3 Answers 3

4

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?

answered Feb 29, 2016 at 21:24
Sign up to request clarification or add additional context in comments.

Comments

1

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

answered Feb 29, 2016 at 20:28

Comments

1

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

answered Feb 29, 2016 at 20:34

1 Comment

raw_input doesn't exist in Python 3

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.