Create directories and modify files with Python
rurpy at yahoo.com
rurpy at yahoo.com
Tue May 1 11:34:57 EDT 2012
On 04/30/2012 05:24 PM, deltaquattro at gmail.com wrote:
> 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,
Perhaps something like this will get you started? To
keep things simple (since this is illustrative code)
there is little parameterization and no error handling.
Apologies if Google screws up the formatting too bad.
--------------------------------------------------------
from __future__ import print_function #1
import os
def main():
listf = open ('foobar.fo')
for line in listf:
dirname, param = line.strip().split() #7
make_directory (dirname, param)
def make_directory (dirname, param):
os.mkdir (dirname) #11
tmplf = open ("input.in")
newf = open (dirname + '/' + 'input.in', 'w') #13
for line in tmplf:
if line.startswith ('foo = ') or line.startswith ('bar = '): #15
line = line.replace (' 1.5 ', ' '+param+' ') #16
print (line, file=newf, end='') #17
if __name__ == '__main__': main() #19
------------------------------------------------------------
#1: Not sure whether you're using Python 2 or 3. I ran
this on Python 2.7 and think it will run on Python 3 if
you remove this line.
#7:The strip() method removes the '\n' characters from
the end of the lines as well as any other extraneous
leading or trailing whitespace. The split() method
here breaks the line into two pieces on the whitespace
in the middle. See
http://docs.python.org/library/stdtypes.html#string-methods
#11: This will create subdirectory 'dirname' relative
to the current directory of course. See
http://docs.python.org/library/os.html#os.mkdir
#13: Usually, is is more portable to use os.path.join() to
concatenate path components but since you stated you are
on Linux (and "/" works on Windows too), creating the path
with "/" is easier to follow in this example. For open()
see
http://docs.python.org/library/functions.html#open
#15: Depending on your data, you might want to use the re
(regular expression) module here if the simple string
substitution is not sufficient.
#16: For simplicity I just blindly replaced the " 1.5 "
text in the string. Depending of your files, you might
want to parameterize this of do something more robust or
sophisticated.
#17: Since we did not strip the trailing '\n' of the lines
we read from "input.in", we use "end=''" to prevent
print from adding an additional '\n'. See
http://docs.python.org/library/functions.html#print
#19: This line is required to actually get your python
file to do anything. :-)
Hope this gets you started. I think you will find doing
this kind of thing in Python is much easier in the long
run than with bash scripts.
A decent resource for learning the basics of Python is
the standard Python tutorial:
http://docs.python.org/tutorial/index.html
More information about the Python-list
mailing list