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

  • SEARCH
  • Home
  • 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 7968915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:09:11+00:00 2026-06-04T07:09:11+00:00

I am trying to write a method that will accept either an opened file

  • 0

I am trying to write a method that will accept either an opened file

myFile = open("myFile.txt")
obj.writeTo(myFile)
myFile.close()

or a string with a path

obj.writeTo("myFile.txt")

The method is implemented as follows:

def writeTo(self, hessianFile):
    if isinstance(hessianFile,file):
        print("File type")
    elif isinstance(hessianFile,str):
        print("String type")
    else:
        pass

But this raises an error

NameError: global name 'file' is not defined

why is file type not defined? Shouldn’t file be defined all the time? How should the implementation be corrected to properly handel both path an file as valid argument types

  • 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-06-04T07:09:12+00:00Added an answer on June 4, 2026 at 7:09 am

    Don’t type check! It’s not Pythonic. The core of duck typing is the idea that if it quacks like a duck, it is a duck. The behaviour you want is if it is file-like, it works, and if it is string-like, it works. This isn’t just ideological – because this is the standard in Python, people will expect to be able to give you a file-like object and have it work. If you restrict it to only a specific file type, your code will be fragile and inflexible.

    The simplest option is to simply pick the more common outcome, try to work as you would with that, and fail to the other method if you get an exception:

    def writeTo(self, hessianFile):
        try:
            with open(hessianFile, "w") as f:
                do_stuff(f)
        except TypeError:
            do_stuff(hessianFile)
    

    This might seem bad if you are used to other languages where “using exceptions for flow control” is considered bad, but that isn’t the case in Python, where they are a core part of the language regularly used that way (every for loop ends with an exception!).

    Or, if you think you are more likely to get a file object most of the time, do it the other way around:

    def writeTo(self, hessianFile):
        try:
            do_stuff(f)
        except AttributeError:
            with open(hessianFile, "w") as f:
                do_stuff(f)
    

    Note my use of the with statement which is the best way of dealing with opening files – it’s more readable, and also always closes the file for you, even on exceptions.

    If you really find you have to type check (e.g: the operation is extremely expensive even if it fails, with no way to short-circuit), you should check the string side, as it is easier to work out if something is string-like as opposed to file-like. If you have to check for something file-like, you should implement an abstract base class and look for the functionality you need, rather than actually type-checking.

    The reason your original code failed is that file isn’t the base class of objects returned by open() in 3.x.

    The type of file object returned by the open() function depends on the
    mode. When open() is used to open a file in a text mode (‘w’, ‘r’,
    ‘wt’, ‘rt’, etc.), it returns a subclass of io.TextIOBase
    (specifically io.TextIOWrapper). When used to open a file in a binary
    mode with buffering, the returned class is a subclass of
    io.BufferedIOBase. The exact class varies: in read binary mode, it
    returns a io.BufferedReader; in write binary and append binary modes,
    it returns a io.BufferedWriter, and in read/write mode, it returns a
    io.BufferedRandom. When buffering is disabled, the raw stream, a
    subclass of io.RawIOBase, io.FileIO, is returned. Source

    So for that you want io.FileIO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a method that makes a log.txt file if one
I am trying to write a generic method that will search a file for
I am trying to write an Audit Log method that will log all changes
I'm trying to write an extension method for objects that will dump the structure
All, I am trying to write a cross platform (hence: boost) method/function that will
Im trying to write a method that will pull out the ID from a
I am trying to write a method that will toggle a button from being
I'm trying to write an extension method that will give me the MemberInfo representing
I'm trying to write a method that will get a private field in a
I'm trying to write a method that will take in two Queues (pre-sorted Linked

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.