Oserror: [Winerror 123] the Filename, Directory Name, or Volume Label Syntax Is Incorrect: '<Frozen Importlib._Bootstrap>' (Django)

DJango Error - [WinError 123] The filename, directory name, or volume label syntax is incorrect: '<frozen importlib._bootstrap>'

Found it. Instead of polls.url, I wrote poll.url inside the url.py for the mysite package. Watch your spelling!

How to solve OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect error in pycharm

The problem could be in your .urls especially if you were following the Django documentation, copying the code snippets and forgot to change the URL details to match your application settings.

In my case, for example, I had this:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
path('crime_system/', include('polls.urls')),
path('admin/', admin.site.urls),
]

OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: [Python]

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))


Related Topics



Leave a reply



Submit