1

I am using a find statement to execute python scripts which are in sub folders of all sub folders of the current directory

The find statement is find . -name \process.py -type f -exec python3 {} \;

The problem I am encountering is that the script is using relative paths e.g ..\data for retrieving other resources. These relative paths are resolved as required when the script is executed individually by running it from its directory but when running the script from a parent directory two levels up using the find command the path resolves relative to that parent directory causing errors

1
  • The answer was found here here Commented Jun 2, 2018 at 1:51

2 Answers 2

1

You can use -execdir option of the find command:

Having the following folder structure in /tmp:

/test
/test/subdir1
/test/subdir1/subdir2
/test/subdir1/subdir2/subdir3

and a r.py file in each folder:

# r.py
import os
dirpath = os.path.dirname(os.path.realpath(__file__))
print(dirpath)

You have the output:

/tmp/test
/tmp/test/subdir1
/tmp/test/subdir1/subdir2
/tmp/test/subdir1/subdir2/subdir3
Sign up to request clarification or add additional context in comments.

Comments

0

If you need to do this in python I suggest looking at this answer: How to get an absolute file path in Python

It is always better to find an absolute path, If using bash:

readlink -f filename 

Comments

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.