1 year ago

#368395

test-img

David Smerdon

Moving and editing multiple text files in a loop in python

I have a directory with 71 folders named lifecyc1, lifecyc2, ..., lifecyc71. Each folder contains a folder /data and a text file script1.sh. The script is short:

#!/bin/bash
#PBS -A UQ-BEL-Economics
#PBS -N lifecyc1
#PBS -l select=1:ncpus=22:mem=16gb
#PBS -l walltime=15:00:00

I need to do two things:

  1. For each folder i called lifecyci, change the third line of the script script1.sh in that folder from #PBS -N lifecyc1 to #PBS -N lifecyci and rename the file to scripti.sh, where i is the folder number from 1 to 71. E.g. for folder 2, edit the line to #PBS -N lifecyc2 and rename the file to script2.sh.
  2. Move files named datai.pgn from a directory PATH into the corresponding lifecyci/data, where i is the folder number. E.g. move PATH/data1.pgn into lifecyc1/data, move PATH/data2.pgn into lifecyc2/data, etc.

I would like to do this in python, or in Terminal if that is easier.

python

loops

rename

batch-processing

edit

0 Answers

Your Answer

Accepted video resources