[Solved] Python WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect:

I am new to programming, this is actually my first work assignment with coding. my code below is throwing an error:

WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect.

I’m not able to find where the issue is.

import os

folders = ["pdcom1", "pdcom1reg", "pdcomopen"]


for folder in folders:
    path = r'"C:AppsCorVuDATAReportsAlliDMonthly Commission ReportsOutputpdcom1"'
    for file in os.listdir(path):
        print file
Solution #1:

As it solved the problem, I put it as an answer.

Don’t use single and double quotes, especially when you define a raw string with r in front of it.

The correct call is then

path = r"C:AppsCorVuDATAReportsAlliDMonthly Commission ReportsOutputpdcom1"

or

path = r'C:AppsCorVuDATAReportsAlliDMonthly Commission ReportsOutputpdcom1'
Respondent: jkalden
Solution #2:

I had a related issue working within Spyder, but the problem seems to be the relationship between the escape character ( “”) and the “” in the path name
Here’s my illustration and solution (note single vs double \ ):

path =   'C:UsersmyUserNameprojectsubfolder'
path   # 'C:\Users\myUserName\projectsubfolder'
os.listdir(path)              # gives windows error
path =   'C:\Users\myUserName\project\subfolder'
os.listdir(path)              # gives expected behavior
Respondent: Kevin Burns
Solution #3:

I had a similar issue while working with Jupyter.
I was trying to copy files from one directory to another using copy function of shutil.

The problem was that I had forgotten to import the package.(Silly) But instead of python giving import error, it gave this error.

Solved by adding:

from shutil import copy
Respondent: user8996561
Solution #4:

I had this problem with Django and it was because I had forgotten to start the virtual environment on the backend.

Respondent: Peter Smiley
Solution #5:

I was facing same error with Django Rest Framework, It was nothing to do with UI, still was getting this error.
I applied below solution, worked for me.

  • Restarted Machine.
  • Restarted Virtual Environment.
Respondent: Omkar
Solution #6:

This is kind of an old question but I wanted to mentioned here the pathlib library in Python3.

If you write:

from pathlib import Path
path: str = 'C:\Users\myUserName\project\subfolder'
osDir = Path(path)

or

path: str = "C:\Users\myUserName\project\subfolder"
osDir = Path(path)

osDir will be the same result.

Also if you write it as:

path: str = "subfolder"
osDir = Path(path)
absolutePath: str = str(Path.absolute(osDir))

you will get back the absolute directory as

'C:\Users\myUserName\project\subfolder'

You can check more for the pathlib library here.

Respondent: bad_locality
Solution #7:

execute below

Python manage.py makemigrations

It will show missing package.

Install missing package and again run below command to make sure if nothing is missed.

Python manage.py makemigrations

It will resolve your issue.

Solution #8:

I had the same error though I did not use the path as in your case. The solution to my problem was to install the django-extension package.

Respondent: Wenxu
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.