0

I have multiple video files in a directory. Their name contains multiple useless underscores(_).

Ex: How___are_you__doing.

I want to change it to: How are you doing.

I have tried few python codes but couldn't get it right, I am novice programmer.

2
  • Show us what you have attempted so far, Commented Nov 8, 2020 at 15:38
  • 1
    To clarify, you're trying to rename files by replacing underscores in their file names with spaces, correct? Commented Nov 8, 2020 at 17:05

3 Answers 3

1

What about:

import re
mytitle = "How___are_you__doing."
re.sub("[_]+"," ", mytitle)

Output:

How are you doing.
Sign up to request clarification or add additional context in comments.

Comments

0

An alternative with replace and join:

text = "How___are_you__doing."
text = text.replace("_", " ")
text = ' '.join(text.split())

Output:

How are you doing.

Comments

0

Ok Guys, I finally made it worked. I found this code on GeeksForGeeks, I modified it a little bit to get done what I wanted.

import os
def main():
   path="C:/Users/TP/Desktop/sample/Travel/west bengal/bishnupur/" //paste your dir path here
   for filename in os.listdir(path):
      my_source =path + filename
      my_dest = filename.rename('_',' ')
      my_dest =path + my_dest
      os.rename(my_source, my_dest)
if __name__ == '__main__':
   main()

But It replaced continuous underscores with continuous whitespaces. I am yet to figure out that.

1 Comment

ah well, maybe the answer is right in front of you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.