0

Okay, guys. I am having a problem. I have a file name 'My Song.mp3' which I wanted to rename it to something with whatever is stored in a variable and with different extention as well.

But whenever I am trying to rename it with code. it gave me an error of FileNotFoundError. Then I realized the actual file name is appearing as \u202aMy Song\u202c\u200f.mp3. I believe it has something to do with special characters or Unicode.

    mp3_file= title + ".mp3"
    base = os.path.split(mp3_file)[0]
    os.rename(mp3_file, base + title + '.mp4')

Error

FileNotFoundError: [WinError 2] The system cannot find the file specified: \u202amysong\u202c\u200f.mp3' -> '\u202mysong2020\u202c\u200f.mp4'`
EeeJay
  • 15
  • 8
  • Does this answer your question? [How can I remove non-ASCII characters but leave periods and spaces using Python?](https://stackoverflow.com/questions/8689795/how-can-i-remove-non-ascii-characters-but-leave-periods-and-spaces-using-python) – DYZ Apr 10 '20 at 16:01

1 Answers1

0

The folowing retains only ASCII characters:

"".join([char for char in filename if ord(char) < 128])
#'My Song.mp3'
DYZ
  • 55,249
  • 10
  • 64
  • 93