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
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.