On 04-11-2005, Aaron wrote:
> Hi all,
> I have a pthon script with a function in it that I have used for
> renaming single files.
> 
> I now want to do it on a series of files.
> 
> the main program that uses the function is:
> 
> #--------------------------
> # M a i n   P r o g r a m
> #--------------------------
> # Test run
> 
> # strLilyname = lilynameGen( '384-3.ly' ) 
> # - Commented this test out - it generates a lilyname from the file
> # strLilyname = lilynameAssign( '384-3.ly', strLilyname )
> # - Commented this test out - it assigns the lilyname to the lily file
> 
> # This is the test run for the 'batch' function ...
> strLilyname = lilynameGenAssign( '[a-z].ly', 'COPY' )
> #this is what I tried to have it work on all files but I 
> # obviously have not idea of the correct way to to this
> # Alternatively test run:
> # strLilyname = lilynameGenAssign( '', 'RENAME' )
> print "========================"
> print "Lily name =", strLilyname
> 
> --------------------------------------------------------------
> What should I use to generate this script for a series of files?

It's not obvious what the functions you call do.  I'd be clearer if you
posted all the code.  I will assume `lilynameGen(name)` returns a new
name for a file named `name`.  So a script to rename one file  might
be::

  #!/usr/bin/env python
  def lilynameGen(name):
      ... your rename logic here ...
      return newname
  
  import sys, shutil
  
  name = sys.argv[1]  # first command-line arg (argv[0] is script name)
  shutil.move(name, lilynameGen(name))

If you want to work on a list of files, just use a for loop at the end::

  for name in sys.argv[1:]:  # all command-line args
      shutil.move(name, lilynameGen(name))

This script can be run with a list of names, e.g.::

  $ script.py a.ly b.ly

If you use glob patterns in the command line::

  $ script.py *.ly

the shell expands them to a list of arguments - the script never knows
you didn't type them one by one.  If you don't get names from the
command line and want to expand glob patterns inside python code, you
can use the glob module::

  import glob
  for name in glob.glob('*.ly'):
      shutil.move(name, lilynameGen(name))

Hope that helped.  If you have more questions, please post the entire
code you wrote and/or state the precise behavior you need.

And post python question to python@linux.org.il to get quicker help.



=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to