Friday, March 6, 2009

Greyhound Bus Cambridge On

Rename Files Rename Files

Here we are. This is my first post ... in Inglese. We Can Start From Some little script Which sitrng delete from file-names:

import os

daSostituire = raw_input ("Enter the string to replace>") =

SostituisciCon raw_input ("Enter the string to replace> ")

for path in os.listdir (os.getcwd ()): = NewName

path.replace (daSostituire, SostituisciCon)

os.rename (path, newname)

print "I renamed% s to% s"% (path, newname)

Well, now we'll see what we Have Done:

import os : I load the package to mange files and folders.

daSostituire = raw_input ("Enter the string to replace>") : Ask the user to the string to replace.

SostituisciCon = raw_input ("Enter the string to replace>") : Ask the user to the string That has to be used to replace the above.

os.getcwd () : this Gives me the folder of the script.

os.listdir () : this one Gives me the list of files and folders.

for ... in ... : this is a cycle and takes all the element in the list. I.e.: if I want all the apples in a bag I'll do: for apple in bag.

for path in os.listdir(os.getcwd()) : this does a cycle on all files and folders in the current directory.

nuovoNome = path.replace(daSostituire,SostituisciCon) : makes the new name of the file, by replacing the string defined above.

os.rename(path,nuovoNome) : renames the file.

print "Ho rinominato %s in %s"%(path,nuovoNome) : prints what it did.

Warning: this code is only as example if you want a most useful script goes here

0 comments:

Post a Comment