Created
October 27, 2019 23:15
-
-
Save ccarouge/24b1419cfd009900e5f618f615bd25db to your computer and use it in GitHub Desktop.
Add line to a bunch of files and run the files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import pathlib | |
import subprocess | |
# Get the list of files we want to modify | |
p=pathlib.Path('mom6') | |
filelist=list(p.glob('**/sync_output_to_gdata.sh')) | |
# Loop on the files | |
for f in filelist: | |
# Add the line | |
with f.open('a') as fo: | |
# I've added an 'echo' for testing but feel free to remove it | |
fo.write('echo rsync --ignore-existing --exclude ".nc." -vrltoD --safe-links restart*0 ${GDATADIR}\n') | |
# Make sure file is executable | |
f.chmod(0o700) | |
# Execute script | |
subprocess.run(str(f)) |
Have you tried simply replacing subprocess.run(str(f))
in the initial gist with subprocess.run(str(f), cwd=str(f.parent))
?
Just thinking this might not work as f will have the path in as well. Try:
subprocess.run(str(f.name),cwd=str(f.parent))
thanks @ccarouge
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@aidanheerdegen,
gives error
This did the job