Create directories and modify files with Python

deltaquattro at gmail.com deltaquattro at gmail.com
Mon Apr 30 19:24:51 EDT 2012


Hi,

I would like to automate some simple tasks I'm doing by hand. Given a text file 
foobar.fo:

073 1.819
085 2.132
100 2.456
115 2.789

I need to create the directories 073, 085, 100, 115, and copy in each directory a modified version of the text file input.in:

.
.
.
foo = 1.5 ! edit this value 
.
.
.
bar = 1.5 ! this one, too
.
.
.

Tthe modification consists in substituting the number in the above lines with the value associated to the directory in the file foobar.fo. Thus, the input.in file in the directory 100 will be:

.
.
.
foo = 2.456 ! edit this value 
.
.
.
bar = 2.456 ! this one, too
.
.
.

At first, I tried to write a bash script to do this. However, when and if the script will work, I'll probably want to add more features to automate some other tasks. So I thought about using some other language, to have a more flexible and mantainable code. I've been told that both Python and perl are well suited for such tasks, but unfortunately I know neither of them. Can you show me how to write the script in Python? Thanks,

Best Regards

Sergio Rossi








More information about the Python-list mailing list