Here is the relevant code:
for file in files:
with readfile(file) as openfile:
molecules.append(process_file_fn(openfile))
and I am getting this error from the code above:
src/datamodules/components/edm/process.py", line 92, in process_xyz_files with readfile(file) as openfile: AttributeError: __enter__
Here is the definition of the readfile:
if tarfile.is_tarfile(data):
tardata = tarfile.open(data, "r")
files = tardata.getmembers()
def readfile(data_pt):
return tardata.extractfile(data_pt)
My data is 1234.xyz.tar.bz2
Any insights/suggestions for me is appreciated. Thank you in advance
I tried to define the mode which is read in both the function and the loop but I am met with the same error.
You need to write a class with the
__enter__and__exit__methods to use awithstatement like that.Have a look at the responses here: Implementing use of 'with object() as f' in custom class in python
If you don't want to implement the context manager you could try changing your for loop to: