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 1092991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:45:53+00:00 2026-05-16T23:45:53+00:00

I have to open a file-like object in python (it’s a serial connection through

  • 0

I have to open a file-like object in python (it’s a serial connection through /dev/) and then close it. This is done several times in several methods of my class. How I WAS doing it was opening the file in the constructor, and then closing it in the destructor. I’m getting weird errors though and I think it has to do with the garbage collector and such, I’m still not used to not knowing exactly when my objects are being deleted =\

The reason I was doing this is because I have to use tcsetattr with a bunch of parameters each time I open it and it gets annoying doing all that all over the place. So I want to implement an inner class to handle all that so I can use it doing
with Meter('/dev/ttyS2') as m:

I was looking online and I couldn’t find a really good answer on how the with syntax is implemented. I saw that it uses the __enter__(self) and __exit(self)__ methods. But is all I have to do implement those methods and I can use the with syntax? Or is there more to it?

Is there either an example on how to do this or some documentation on how it’s implemented on file objects already that I can look at?

  • 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-16T23:45:54+00:00Added an answer on May 16, 2026 at 11:45 pm

    Those methods are pretty much all you need for making the object work with with statement.

    In __enter__ you have to return the file object after opening it and setting it up.

    In __exit__ you have to close the file object. The code for writing to it will be in the with statement body.

    MODE = 'rb'
    class Meter():
        def __init__(self, dev):
            self.dev = dev
        def __enter__(self):
            #ttysetattr etc goes here before opening and returning the file object
            self.fd = open(self.dev, MODE)
            return self
        def __exit__(self, exception_type, exception_value, exception_traceback):
            #Exception handling here
            self.fd.close()
    
    meter = Meter('/dev/tty0')
    with meter as m:
        #here you work with the file object.
        m.fd.read()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file containing python's object as string, then i open it and
I have one line in php file like this: <a class=hide-if-no-js onclick=findPosts.open( 'media[]','<?php echo
I have created a buffer object in python like so: f = io.open('some_file', 'rb')
I have an WinMo app and I would like to open a file from
i dont have microsoft access but would like to open an mdb file, is
I have open pdf file through my application.when click on device back button it
I have a open file dialog with three filters: QString fileName = QFileDialog::getOpenFileName( this,
I have a csv DictReader object (using Python 3.1), but I would like to
I have a file csv file in this format, I would like to generate
I have a file like this: declare a = aexpress b = bexpress begin

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.