Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Creating a directory structure and modifying files automatically in Python

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


deltaquattro at gmail

Apr 30, 2012, 8:19 AM

Post #1 of 9 (743 views)
Permalink
Creating a directory structure and modifying files automatically in Python

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,

Best Regards

Sergio Rossi
--
http://mail.python.org/mailman/listinfo/python-list


python at mrabarnett

Apr 30, 2012, 9:08 AM

Post #2 of 9 (726 views)
Permalink
Re: Creating a directory structure and modifying files automatically in Python [In reply to]

On 30/04/2012 16:19, deltaquattro [at] gmail 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,
>
If you don't know Python, could I suggest that you start by reading a
tutorial such as "Dive Into Python", which is at
http://www.diveintopython.net/
--
http://mail.python.org/mailman/listinfo/python-list


clp2 at rebertia

Apr 30, 2012, 9:49 AM

Post #3 of 9 (728 views)
Permalink
Re: Creating a directory structure and modifying files automatically in Python [In reply to]

On Mon, Apr 30, 2012 at 8:19 AM, <deltaquattro [at] gmail> 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
--
http://mail.python.org/mailman/listinfo/python-list


nagle at animats

Apr 30, 2012, 10:54 PM

Post #4 of 9 (719 views)
Permalink
Re: Creating a directory structure and modifying files automatically in Python [In reply to]

On 4/30/2012 8:19 AM, deltaquattro [at] gmail 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:

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
--
http://mail.python.org/mailman/listinfo/python-list


nospam at nospam

May 6, 2012, 8:08 PM

Post #5 of 9 (695 views)
Permalink
Re: Creating a directory structure and modifying files automatically in Python [In reply to]

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




John Nagle <nagle [at] animats> wrote:
> On 4/30/2012 8:19 AM, deltaquattro [at] gmail 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:
>
--
http://mail.python.org/mailman/listinfo/python-list


no.email at nospam

May 6, 2012, 9:59 PM

Post #6 of 9 (693 views)
Permalink
Re: Creating a directory structure and modifying files automatically in Python [In reply to]

Javier <nospam [at] nospam> writes:
> 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.
--
http://mail.python.org/mailman/listinfo/python-list


nagle at animats

May 7, 2012, 8:46 PM

Post #7 of 9 (695 views)
Permalink
Re: Creating a directory structure and modifying files automatically in Python [In reply to]

On 5/6/2012 9:59 PM, Paul Rubin wrote:
> Javier<nospam [at] nospam> writes:
>> 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.

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

John Nagle

--
http://mail.python.org/mailman/listinfo/python-list


showell30 at yahoo

May 7, 2012, 9:09 PM

Post #8 of 9 (689 views)
Permalink
Re: Creating a directory structure and modifying files automatically in Python [In reply to]

On May 7, 8:46 pm, John Nagle <na...@animats.com> wrote:
> On 5/6/2012 9:59 PM, Paul Rubin wrote:
>
> > Javier<nos...@nospam.com>  writes:
> >> 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.
>
>     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.




--
http://mail.python.org/mailman/listinfo/python-list


nagle at animats

May 7, 2012, 9:50 PM

Post #9 of 9 (689 views)
Permalink
Re: Creating a directory structure and modifying files automatically in Python [In reply to]

On 5/7/2012 9:09 PM, Steve Howell wrote:
> On May 7, 8:46 pm, John Nagle<na...@animats.com> wrote:
>> On 5/6/2012 9:59 PM, Paul Rubin wrote:
>>
>>> Javier<nos...@nospam.com> writes:
>>>> 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.
>>
>> 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.

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
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.