top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Define a class containing methods for reading a file and then store lines in external variable

+1 vote
424 views

I want to build a class that perform various functions to the content(or lines) of any given file. I want to first include the opening and reading file function into that class, then add other methods.

Here is what I wrote for opening file and reading the lines:

class FileOpration:
 def __init__(self,name):
 self.name = name
 self.filename = self.name
 def readAllline(self):
 open(self.name).readlines()

file1 = FileOpration("myfile.txt")
print file1.filename
allines = file1.readAllline()
print allines

I define self.name variable because I want to store the specific file that I read. This works fine by the test code of print file1.filename. I saw "myfile.txt" on the terminal output.

However, the readAllline() method dose not work. The code only give me "None" as the output on terminal

posted Aug 15, 2013 by Garima Jain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

You probably want to return that value:

def readAllline(self):
 return open(self.name).readlines()

Then it'll do what you expect. But why wrap this up in a class? Why not simply call the underlying functions directly?

answer Aug 15, 2013 by Amit Parthsarthi
Similar Questions
+2 votes

I would like to apply the Pool.map method to a member of a class. Here is a small example that shows what I would like to do:

from multiprocessing import Pool

class A(object):
 def __init__(self,x):
 self.value = x
 def fun(self,x):
 return self.value**x

l = range(10)

p = Pool(4)

op = p.map(A.fun,l)

using this with the normal map doesn't cause any problem

This fails because it says that the methods can't be pickled. (I assume it has something to do with the note in the documentation: "functionality within this package requires that the __main__ module be importable by the children.", which is obscure to me).

I would like to understand two things: why my code fails and when I can expect it to fail? what is a possible workaround?

0 votes

I have a hierarchy of classes (SubA, SubAB, SubB, ..., SubBCA, SubC,...), each of which is inheriting from a chain of superclasses with a common baseclass(Sup) on top. (So far, no problem)

Now, I want to create instances of the correct subclasstype as decided by the common baseclass, like this:

i = Sup(args_allowing_the_baseclass_to_deduce_correct_subclass)

where i can be of any class except Sup itself (as decided by Sup)

Now, the problem:

How to design the __new__() and __init__() methods for the various classes in order to achieve what I want?

+3 votes

Is there way to get list of instances of particular class through class itself? via metaclass or any other method?

If class is object is it possible to delete it? If it is possible then how instances of that class will behave?

0 votes

I am running a python script and it will create a file name like filename0.0.0 and If I run it again then new file will create one more like filename0.0.1......
my code is-

i = 0
for i in range(1000):
 try:
 with open('filename%d.%d.%d.json'%(0,0,i,)): pass
 continue
 except IOError:
 dataFile = file('filename%d.%d.%d.json'%(0,0,i,), 'a+')
 break

But It will take more time after creating many files, So i want to store value of last var "i" in a variable so that when I run my script again then I can use it. for example-
my last created file is filename0.0.27 then it should store 27 in a variable and when i run again then new file should be created 0.0.28 according to last value "27", so that i could save time and it can create file fast..

...