Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sending a file path from .py to another .py

I’m trying to call a foo.py passing a filePath to bar.py

foo.py

for path in Path('./csvs').rglob('*.csv'):
    exec(open('bar.py').read())
    exec(open('bar2.py').read())
    exec(open('bar3.py').read())

bar.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df = pd.read_csv(path, usecols=cols)
#rest of code

Is it possible?

>Solution :

After .read()ing a file you’re dealing with a string. So you can .replace() a placeholder.

file1.py:

exec(open("file2.py").read().replace("*place_holder*", "10"))

file2.py:

def fn():
    print("My number is *place_holder*")
fn()

But be careful about what you choose as the placeholder. .replace() will replace all the occurrences.

Of course for more complex replacing you can use regex. It can restrict the search better and safer.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading