sorting a file

Terry Reedy tjreedy at udel.edu
Thu Dec 5 17:34:34 EST 2002


"Romualdo" <micyaro at pf.pl> wrote in message
news:946575c0.0212050808.70c1e246 at posting.google.com...
> Hello,
>
> I am trying to rewrite a little shell script into python.
> I ve got a text file with following content:
>
> foo bar file_name1.ext ./foobar
> foo bar file_name2.ext ./foobar
> ...
> Now the shell script reads only the "file_nameXX.ext"-part and using
> it as a variable, the script moves the file "file_nameXX.ext" from a
> directory to another directory given. It looks like this:
>
> source_dir = /foo/bar
> dest_dir = /another/foo/bar
> sourcefile = "source_dir" + "/" + "file_name"
> destfile = "dest_dir" + "/" + "file_name"
> PROGRAM = move sourcefile destfile
>
> Does anybody could tell me how the lines above would look like in
> python?
> Thank you for helping me out.

I would do something like (UNTESTED!, but gives idea):

import shutil
for line in file('movelist.txt'):
  d1,d2,f,t = line.split() # assuming all lines have 4 fields
  src = '/%s/%s/%s' % (d1,d2,f)
  dst = '/another/%s/%s/%s' % (d1,d2,f) # its not clear what
'./foobar/ is about
  shutil.filecopy(src,dst) #not sure of method name

TJR






More information about the Python-list mailing list