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)) |
Duh! I just remembered there is a cwd
argument for subprocess
, so
subprocess.run(str(f), cwd=str(f.parent))
would work and is much cleaner
Also, you need to change the definition of p at the start to be an absolute path:
p=pathlib.Path('/short/v45/nc3060/mom6')
or whatever path you need.
I guess you don't need the absolute path at the start, but it is much safer to use one.
subprocess.run(str(f))
gives error
subprocess.run(str(f))
File "/g/data3/hh5/public/apps/miniconda3/envs/analysis3-19.07/lib/python3.6/subprocess.py", line 403, in run
with Popen(*popenargs, **kwargs) as process:
File "/g/data3/hh5/public/apps/miniconda3/envs/analysis3-19.07/lib/python3.6/subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "/g/data3/hh5/public/apps/miniconda3/envs/analysis3-19.07/lib/python3.6/subprocess.py", line 1344, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'layer4/layer4_tau1e-1_manyshortridges/sync_output_to_gdata.sh': 'layer4/layer4_tau1e-1_manyshortridges/sync_output_to_gdata.sh'
This did the job
subprocess.run('./sync_output_to_gdata.sh')
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
Navid pointed out that the subprocess call has to happen within each folder, so something like this would work:
and then change to