3

I want to rename an mp3 file.

   os.rename(f'C:\\Users\\axeld\\Desktop\\Music\\NG  Trial\\{item}',
             f'C:\\Users\\axeld\\Desktop\\Music\\NG  Trial\\{Song_name}')

But I get this error:

OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\Users\\axeld\\Desktop\\Music\\NG  Trial\\109650.mp3' -> 'C:\\Users\\axeld\\Desktop\\Music\\NG  Trial\\Operation: Evolution.mp3'

I am a 100% sure that the file is there, so why am I getting this error?

Maypher
  • 121
  • 1
  • 2
  • 10

1 Answers1

1

I don't have a Windows box to try this on, but have you considered using os.path.join to create paths?

basedir = os.path.join('C:/', 'Users', 'axeld', 'Desktop', 'Music', 'NG  Trial')
old_name = os.path.join(basedir, item)
new_name = os.path.join(basedir, song_name)
os.rename(old_name, new_name)

From the documentation of os.path.join:

Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator (os.sep) following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.

On Windows, the drive letter is not reset when an absolute path component (e.g., r'\foo') is encountered. If a component contains a drive letter, all previous components are thrown away and the drive letter is reset. Note that since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.

Notice the very last line, which documents a special case on Windows (see also this answer on SO: that's why there's forward-slash after C: in my code above.

Alternative solution

Based on the comments, the os.path.join solution would still incur in errors. As a workaround, you can use raw strings:

os.rename(
    r'C:\Users\axeld\Desktop\Music\NG Trial\{}'.format(item), 
    r'C:\Users\axeld\Desktop\Music\NG Trial\{}'.format(song_name))
Community
  • 1
  • 1
Jir
  • 2,985
  • 8
  • 44
  • 66
  • Interesting... At the risk of stating the obvious, do you really have two spaces between `NG` and `Trial`? Also would you mind trying using raw strings instead? Like this: `os.rename(r'C:\Users\axeld\Desktop\Music\NG Trial\{}'.format(item), r'C:\Users\axeld\Desktop\Music\NG Trial\{}'.format(Song_name))` – Jir Feb 24 '20 at 13:29
  • I tried that and now I get this error: `FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\axeld\\Desktop\\Music\\NGCopy\\183725.mp3' -> 'C:\\Users\\axeld\\Desktop\\Music\\NGCopy\\"\\\\Rock the House".mp3' ` – Maypher Feb 24 '20 at 14:07
  • Even though I'd tend to think the error is related to the file of origin, there are some dubious double quotes and backslashes in the new filename. To keep things simple, can you try renaming `183725.mp3` to something like `183725_test` (that is, using `item` and `item + '_test'`)? – Jir Feb 24 '20 at 14:19
  • (Ignore the comment above)It worked. Now I'm trying to change the file tags but I get this error:`audio.tag.artist = Author_name AttributeError: 'NoneType' object has no attribute 'artist' – Maypher Feb 24 '20 at 14:24
  • Cool, I'll update my answer so, hopefully, it'll help others, too. As for the other error, it looks like the `audio.tag` object does not have any attribute named `artist` - but that's a different question and we might go off topic. – Jir Feb 24 '20 at 14:26