Python Forum
How to read file inside class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to read file inside class
self in the class is the instance itself. If you want to use a instance attribute inside the class you need to use it with self.
class PythonTraining():    
    def read_data(self, filepath):
        base_path = "D:\Data" 
        full_filepath = os.path.join(base_path, filepath, "outsummary2.csv")      = pd.read_csv(full_filepath)

    def cal_rowmean(self, v1):         
        self.xmean =['s3'].mean() + v1
Note, that read_data doesn't need to return
Also, note that you need to call read_data() before you call cal_rowmean or you will get AttributeError. It may be better if you refactor your class and move what reading data in class __init__ method (you don't have defined one now)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

You lose flexibility and most of the point of this if hardcore most of the stuff inside the class.
It's no so easy with Pandas as often need flexibility to change a lot of stuff(also when read in files).

Here a demo,where take stuff in from outside of the class.
The can continue in normal way to work with df DataFrame object.
Movie;Year Seven;1995 The Godfather;1972 Jaws;1975
import pandas as pd
import os

class PythonTraining():
    def __init__(self, base_path, file_name):
        self.base_path = base_path
        self.file_name = file_name

    def read_data(self, sep=';', header='infer'):
        full_filepath = os.path.join(self.base_path, self.file_name) = pd.read_csv(full_filepath, sep=sep, header=header)

if __name__ == '__main__':
    # Now from outside give path and filename
    base_path = r'E:\div_code\home'
    file_name = 'movies.csv'
    df = PythonTraining(base_path, file_name)

    # Give paramater to csv read
    sep = ';'
    #header = None
    df =
As you use Spyder here a Screenshot,see that i can continue to work with df object in IPython console.
[Image: t4EpwK.png]

Possibly Related Threads…
Thread Author Replies Views Last Post
  Read file, reformat and write new file bryanmartin113 1 177 Jun-08-2021, 09:27 PM
Last Post: Larz60+
  Read/Write binary file deanhystad 3 519 Feb-01-2021, 10:29 AM
Last Post: Larz60+
  how to change the range of read CSV file every time python file runs greenpine 6 577 Dec-08-2020, 10:11 PM
Last Post: greenpine
  How to read CSV file one row at the time in a range and some delay in between greenpine 2 655 Nov-20-2020, 02:26 PM
Last Post: greenpine
  can't read QRcode in large file simoneek 0 484 Sep-16-2020, 08:52 AM
Last Post: simoneek
  Practice problem using lambda inside the class jagasrik 3 519 Sep-12-2020, 03:18 PM
Last Post: deanhystad
  Binary File Read Aussie 6 1,554 Sep-03-2020, 03:57 AM
Last Post: deanhystad
  Read strings and numbers in columns from a file suvadip 4 880 Aug-11-2020, 09:37 PM
Last Post: suvadip
  read a parameter after updating it in another class MKS2020 3 730 Jul-25-2020, 08:27 AM
Last Post: MKS2020
  Read/Sort Large text file avoiding line-by-line read using mmep or hdf5 Robotguy 0 572 Jul-22-2020, 08:11 PM
Last Post: Robotguy

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020