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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:21:52+00:00 2026-06-05T23:21:52+00:00

I have written a Python script for merging many data files in a few

  • 0

I have written a Python script for merging many data files in a few different ways. This is my first Python script, really my first attempt at OOP, and I suspect that I’ve been thinking about objects and classes in a way that’s functional, but not optimal.

I created a class for the source files and a subclass for lines in source files that are records. Now, with my new understanding that everything in Python is an object, I suspect that I’ve created unnecessary complexity by creating a class for files, when a built-in type not only exists, but also I’m already using it every time I open a file.

Unfortunately it is not clear to me from the documentation how I would assign new attributes, methods, and subclasses to the built-in type for files. I also do not understand how the file datatype may differ from a class; I simply understand both as “factories” for creating objects with particular properties.

class SrcFile:
   self.name = which  
   self.terminals = set([])

def <a few methods>():
   with open(self.name) as file:
      <do some stuff and return something>

class Record(SrcFile):
      <methods>

for file in files:
   file = SrcFile(file)
   if <conditions on values from SrcFile methods>:
      with open(file) as file:
         for line in file:
            if <regexp match>:
               record = Record(line)
               <apply Record() methods>
               <write to tempfiles>

<merge tempfiles to stdout>
  • 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-05T23:21:54+00:00Added an answer on June 5, 2026 at 11:21 pm

    Unfortunately it is not clear to me from the documentation how I would assign new attributes, methods, and subclasses to the built-in type for files.

    pro tip: you don’t.
    (there might be situations where you could consider tinkering with the built-in file type, but that would be overkill for your current problem)

    Looking at the last part of your example, it seems we could throw away your Record and SrcFile classes and rewrite it like this:

    def check_conditions(file):
        #return true if SrcFile conditions are met
    
    def convert_record(line):
        #generate the string you want to print for the record
    
    for file in files:
        if check_conditions(file):
            with open(file) as file:
                for line in file:
                    if <regexp match>:
                        record = convert_record(line)
                        <write to tempfiles>
    

    Where check_conditions checks the conditions that were contained in your SrcFile class and convert_record generates the output for a Record line.

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

Sidebar

Related Questions

I have written a python script (my very first) that accepts input of a
I have written a python script to process a set of ASCII files within
I have written the following really simple python script to change the desktop wallpaper
i have written this python program. whenever i run the script using parameters like
I have written this simple script in python: import gtk window = gtk.Window() window.set_size_request(800,
I have written a Python script that will generate a series of graphs and
I have written a python script which watches a directory for new subdirectories, and
I have written a python script with methods in it. Now I want to
I have a mechanize script written in python that fills out a web form
Say I have a script written in perl or python. What's the easiest way

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.