Jingtong Zhao
Jingtong Zhao

Reputation: 21

How to run a function with nested function for another folder in Python

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

Upvotes: 0

Views: 167

Answers (1)

Isotope
Isotope

Reputation: 143

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())

Upvotes: 0

Related Questions