Skip to content

Instantly share code, notes, and snippets.

@ccarouge
Created October 27, 2019 23:15
Show Gist options
  • Save ccarouge/24b1419cfd009900e5f618f615bd25db to your computer and use it in GitHub Desktop.
Save ccarouge/24b1419cfd009900e5f618f615bd25db to your computer and use it in GitHub Desktop.
Add line to a bunch of files and run the files
#!/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))
@ccarouge
Copy link
Author

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.

@ccarouge
Copy link
Author

I guess you don't need the absolute path at the start, but it is much safer to use one.

@navidcy
Copy link

navidcy commented Oct 28, 2019

@aidanheerdegen,

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')

@ccarouge
Copy link
Author

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

@navidcy
Copy link

navidcy commented Oct 28, 2019

thanks @ccarouge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment