0

I am trying to go the "/opt/intel/mkl/bin" and list the files and check the presence of some files and come out. I am getting this error. Any help is appreciated.

files = ['mklvars.csh','mklvars.sh']
with os.chdir('/opt/intel/mkl/bin'):
    print('testing')
    if all([os.path.isfile(f)for f in files]):
        print("Installation succesful")
    else:
        print("not succesful")```

1 Answers1

0

You can only use the keyword with ... : with context-manager and os.chdir() just isn't a Context Manager.

You can simply do something like (if you don't need working dir to be restored afterwards)

files = ['mklvars.csh','mklvars.sh']
os.chdir('/opt/intel/mkl/bin')
print('testing')
if all([os.path.isfile(f) for f in files]):
    print("Installation succesful")
else:
    print("not succesful")

But if you need the current working directory to be restored at the end, there are different options you can use.

One example (with https://github.com/jaraco/path):

from path import Path

# Changing the working directory:
files = ['mklvars.csh','mklvars.sh']
with Path('/opt/intel/mkl/bin'):
    print('testing')
    if all([os.path.isfile(f)for f in files]):
        print("Installation succesful")
    else:
        print("not succesful")

Other solutions: checkout this Question

For further reading on context-managers:

Tibebes. M
  • 6,940
  • 5
  • 15
  • 36