0

I have a func_2 nested in func_1, and they perform things in different directories. My current directory is the directory for func_2. I am iterating over the filename in the directory of func_1, but func_2 is performed in a different folder, what should I do?

directory = 'directory of a childfolder containing all the ".pdb" files needed in filename'
def func_1(Chain_1):
  index_1 = 0
  for filename in os.listdir(directory): # filename(str)
    if filename.endswith(".pdb"):
      scores = []
      if index_1 <= 10:
        temp = func_2(Chain_1, filename) # func_2 execute a .cpp file in the mother folder
        scores.append(temp)
        index_1 = index_1 + 1
    else:
      continue

1 Answer 1

0

You could change the working directory whenever you enter your function.

This can be done with the os module:

import os

# get the current working directory
print(os.getcwd())

# change the current working directory
os.chdir('./some/existing/folder')

# current working directory is now the new path
print(os.getcwd())
Sign up to request clarification or add additional context in comments.

4 Comments

But the problem is: filename is got from the child folder, while the func_2 should be performed in the mother folder. filename is an input for func_2.
Why don't you os.chdir('motherfolder') directly befor calling func_2 and change back to os.chdir('childrenfolder') directly after func_2?
I tried it and didn't quite work. It returns error: cannot sparse file "xxxx.pdb"
You might need to adjust the filename paths so that they are relative to the folder used in func_2. You can switch the folder upwards by adding "/../" to the path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.