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
3 Answers 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?
answered Feb 29, 2016 at 21:24
Juan
1,0551 gold badge10 silver badges13 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You need to call main() to run your code. If you do that it works just fine.
answered Feb 29, 2016 at 20:28
Mark Skelton
3,9214 gold badges33 silver badges51 bronze badges
Comments
You need to call main() in order for your code to run.
lang-py
main()isn't called automatically.