0

I need a script to go through all files in all directories and get rid of all special characters, ie: [{!@#$%^&*()-_=+\|]}[{'";:/?.>,<}]. Can anyone help me out?

Thank you!

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432

1 Answers1

1
import glob
for fileName in glob.iglob("/home/**/*", recursive = True):
    for character in specialCharacters:
        fileName = fileName.replace(character, "")

Something like this? Doesn't get the directories but check out the glob docs, I am sure there is a way to get them.

Will
  • 151
  • 6
  • that works for a single character in a single directory. I need all the special characters in all directories striped away. – Joseph Arnold Jun 07 '18 at 19:51
  • Why does it only account for a single character? My loop iterates through a list of special characters and removes any that it finds. And with recursive = True it should recursively search the whole directory tree from /home/. – Will Jun 07 '18 at 20:20
  • What I mean by that, is the loop only strips out lets say, "%" from all the files in a single directory. It will not work, for me at least, if i tell it to do, ("%" + "$" + "#" , ""). Or maybee im just doing it wrong. – Joseph Arnold Jun 07 '18 at 21:46
  • Perhaps I used the glob syntax incorrectly in my example, but glob can certainly iterate through all directories under the root directory that you give to it. I have done it before. – Will Jun 07 '18 at 21:58
  • import glob specialCharacters = ["!" , "@" , "#" , "$" , "%" ,...] for fileName in glob.iglob("/Desktop/test/**/*", recursive = True): for character in specialCharacters: fileName = fileName.replace(character, "") This is what i have as my code. Not sure where i went wrong, but it does not do anything for me. – Joseph Arnold Jun 07 '18 at 22:00
  • Maybe give the os.walk method a shot then: https://stackoverflow.com/questions/14798220/how-can-i-search-sub-folders-using-glob-glob-module-in-python – Will Jun 08 '18 at 10:33
  • Thanks, ill give it a try. – Joseph Arnold Jun 08 '18 at 13:25