Monday, March 2, 2009

Ozark Trail Replacement Air Mattress

String Substituting Substituting Strings

So here we are at the first post. Where to start, I'd say you might start with a small script that removes certain characters 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 see that what I did:

import os : load library to the files and folders

daSostituire = raw_input ("Enter the string to replace>") : ask user to input the string you want to replace

SostituisciCon = raw_input ("Enter the string to replace>") : ask for input to 'string with which the user wants to replace the first

os.getcwd () : This gives me the folder where the script is

os.listdir () : This gives me a list of all files and folders in the path

for ... in ... : This is a loop that takes all the elements in the list. Example: if I wanted to take one by one all the apples in a basket would do: for apple in the basket

for path in os.listdir (os.getcwd ()) : I do a loop over all files and folders in the current directory

NewName = path.replace (daSostituire, SostituisciCon) : I define a new name replacing the strings with previous

os.rename (path, newname) : rename the file

print "renamed% s to% s"% (path, newname) : print to video what it did.

Attention all this code is an example if you want a more functional script download it from here

0 comments:

Post a Comment