Creating a directory structure and modifying files automatically in Python

Chris Rebert clp2 at rebertia.com
Mon Apr 30 12:49:00 EDT 2012


On Mon, Apr 30, 2012 at 8:19 AM,  <deltaquattro at gmail.com> wrote:
> Hi,
>
> I would like to automate the following task under Linux. I need to create a set of directories such as
>
> 075
> 095
> 100
> 125
>
> The directory names may be read from a text file foobar, which also contains a number corresponding to each dir, like this:
>
> 075 1.818
> 095 2.181
> 100 2.579
> 125 3.019
>
>
> In each directory I must copy a text file input.in. This file contains  two lines which need to be edited:
>
> .
> .
> .
> foo = 1.5 !edit me..
> .
> .
> .
> bar = 1.5 !..and me, too
> .
> .
>
> The number after the "=" must be set to the value given in foobar for the corresponding directory
> I thought to write a bash/awk shell script for this. However, if and when the script works, I'll probably start to add more features in order to automate even more tasks. It seems to me that Python or perl would be better suited to write a larger, mantainable script. Unfortunately, I know neither of them. Could you show me how to write the script in Python? Thanks,

Your script would use the following modules:
http://docs.python.org/library/shutil.html
http://docs.python.org/library/os.path.html

Cheers,
Chris



More information about the Python-list mailing list