Creating a directory structure and modifying files automatically in Python

D

deltaquattro

Hi,

I would like to automate the following task under Linux. I need to create aset 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 twolines 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 thecorresponding 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 ofthem. Could you show me how to write the script in Python? Thanks,

Best Regards

Sergio Rossi
 
J

John Nagle

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:

Learn how to use a database. Creating and managing a
big collection of directories to handle small data items is the
wrong approach to data storage.

John Nagle
 
J

Javier

Learn how to use a database. Creating and managing a
big collection of directories to handle small data items is the
wrong approach to data storage.

John Nagle

Or not... Using directories may be a way to do rapid prototyping, and
check quickly how things are going internally, without needing to resort
to complex database interfaces.

Just a quote from the history of usenet:

I wrote the very first version of
netnews as a 150-line shellscript. It
had multiple newsgroups and
cross-posting; newsgroups were
directories and cross-posting was
implemented as multiple links to the
article. It was far too slow to use
for production, but the flexibility
permitted endless experimentation with
the protocol design.
-- Steven M. Bellovin

http://www.linuxtopia.org/online_books/programming_books/art_of_unix_programming/ch14s04_2.html

for the question of the OP:

import string
import os
namefile="..."
for line in open("foobar").readlines()
dirname,number=string.split(line)
os.system("mkdir "+dirname)
f=open(dirname+"/"+namefile,"w")
f.write("TEXT..."+number)
f.close()

Portability can be improved by using os.path or something like that.
 
P

Paul Rubin

Javier said:
Or not... Using directories may be a way to do rapid prototyping, and
check quickly how things are going internally, without needing to resort
to complex database interfaces.

dbm and shelve are extremely simple to use. Using the file system for a
million item db is ridiculous even for prototyping.
 
J

John Nagle

dbm and shelve are extremely simple to use. Using the file system for a
million item db is ridiculous even for prototyping.

Right. Steve Bellovin wrote that back when UNIX didn't have any
database programs, let alone free ones.

John Nagle
 
S

Steve Howell

    Right.  Steve Bellovin wrote that back when UNIX didn't have any
database programs, let alone free ones.

It's kind of sad that the Unix file system doesn't serve as an
effective key-value store at any kind of nontrivial scale. It would
simplify a lot of programming if filenames were keys and file contents
were values.
 
J

John Nagle

It's kind of sad that the Unix file system doesn't serve as an
effective key-value store at any kind of nontrivial scale. It would
simplify a lot of programming if filenames were keys and file contents
were values.

You don't want to go there in a file system. Some people I know
tried that around 1970. "A bit is a file. An ordered collection of
files is a file". Didn't work out.

There are file models other than the UNIX one. Many older systems
had file versioning. Tandem built their file system on top of their
distributed, redundant database system. There are backup systems
where the name of the file is its hash, allowing elimination of
duplicates. Most of the "free online storage" sites do that.

John Nagle
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top