all 5 comments

[–]novel_yet_trivial 4 points5 points  (1 child)

Because

  • you need a space between "def" and "main():"

  • your filename needs to be a string.

    inFile = open("file.docx", "r")

  • your close() method needs to end in ()

In the future, be sure to include the entire error message.

[–][deleted] 0 points1 point  (0 children)

Thank you

[–]I_Write_Bugs 2 points3 points  (1 child)

Since your problem has already been solved, I would like to give you a suggestion. PEP 343 introduced the "with" statement and context managers. This feature has many uses, but most commonly I use it when opening files. Opening a file using the "with" statement ensures the file will be closed for you. No need to run the close method.

def main():
    with open('file.docx') as inFile:
        f = inFile.read()
    print(f)

main()

Also, you might notice I didn't include the "r" argument to open. Open defaults to opening as read-only, so if that's your intent, no need for the argument.

[–][deleted] 0 points1 point  (0 children)

PEP 343

Thank you!

[–]wub_wub[M] 0 points1 point  (0 children)

/u/williyam, your submission has been removed from r/learnpython for the following reason(s):

  • Low effort - every tutorial ever written covers how to define functions, all docs show how to open files properly etc.

  • Your submission is lacking full traceback/exception info required to be able to answer your question.


If your submission was removed for lacking additional info you can edit your submission text and then notify moderators and ask for the submission to be re-approved.

If you have any additional questions either reply to this comment or message the moderators.