Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6710005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:57:00+00:00 2026-05-26T07:57:00+00:00

# Open new file to write file = None try: file = open(filePath, ‘w’)

  • 0
# Open new file to write
file = None
try:
    file = open(filePath, 'w')
except IOError:
    msg = ("Unable to create file on disk.")
    file.close()
    return
finally:
    file.write("Hello World!")
    file.close()

The above code is ripped from a function. One of the user’s system is reporting an error in line:

file.write("Hello World!")

error:

AttributeError: 'NoneType' object has no attribute 'write'

Question is, If python is failed to open given file, ‘except’ block executes and it has to
return, but control is getting transferred to the line that is throwing given error. The value of ‘file’ variable is ‘None’.

Any pointers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:57:00+00:00Added an answer on May 26, 2026 at 7:57 am

    You shouldn’t be writing to the file in the finally block as any exceptions raised there will not be caught by the except block.

    The except block executes if there is an exception raised by the try block. The finally block always executes whatever happens.

    Also, there shouldn’t be any need for initializing the file variable to none.

    The use of return in the except block will not skip the finally block. By its very nature it cannot be skipped, that’s why you want to put your “clean-up” code in there (i.e. closing files).

    So, if you want to use try:except:finally, you should be doing something like this:

    try:
        f = open("file", "w")
        try:
            f.write('Hello World!')
        finally:
            f.close()
    except IOError:
        print 'oops!'
    

    A much cleaner way of doing this is using the with statement:

    try:
        with open("output", "w") as outfile:
            outfile.write('Hello World')
    except IOError:
        print 'oops!'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having this code: using (BinaryWriter writer = new BinaryWriter(File.Open(ProjectPath, FileMode.Create))) { //save something here
When I use FuzzyFinder plugin to open a new file in MacVim, it opens
For example if I open a brand new file in vim it has the
I have the following code: using (BinaryReader br = new BinaryReader( File.Open(FILE_PATH, FileMode.Open, FileAccess.ReadWrite)))
With the following file reading code: using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read,
I rewrite my old code in new style, like below: #old style open(FD,file); #new
When using NERD Tree on MacVim, I can open a file in a new
I have listbox and on click event I open new panel where i change
I have window form in my WPF Application when I open new window from
i m trying to pickup contents of a div and open it in new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.