Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

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:\Apps\CorVu\DATA\Reports\AlliD\Monthly Commission Reports\Output\pdcom1"'
    for file in os.listdir(path):
        print file
                well, that was it!  Thank you.  Had added that outside single quote working a different solution, and I suppose I forgot to remove them.
– AlliDeacon
                Nov 9, 2015 at 21:32
                Debugging tip: Ensure that the data you feed to functions that are seemingly misbehaving is correct. A simple print(path) would have shown that the double quotes were a part of the string's value.
– Aasmund Eldhuset
                Nov 10, 2015 at 2:38

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:\Apps\CorVu\DATA\Reports\AlliD\Monthly Commission Reports\Output\pdcom1"
path = r'C:\Apps\CorVu\DATA\Reports\AlliD\Monthly Commission Reports\Output\pdcom1'
                Did you have the exact same problem? At the time I answered the question, I worked with python 2.7. never tried whether python 3 would behave different...
– jkalden
                Nov 7, 2020 at 19:24
                I use Python 3, but the symptom was the same. I guess it's not clear if it is the same problem because the error messages are quite unspecific.
– Fido
                Nov 7, 2020 at 20:34
                True. I guess the core of my answer („don’t mix the types of quotes“) is valid in general, but it might not be enough in all cases. Can you identify characters which disable my solution? In case yes That would be worth a separate answer!
– jkalden
                Nov 7, 2020 at 20:37

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:\Users\myUserName\project\subfolder'
path   # 'C:\\Users\\myUserName\\project\subfolder'
os.listdir(path)              # gives windows error
path =   'C:\\Users\\myUserName\\project\\subfolder'
os.listdir(path)              # gives expected behavior
                The problem was double quotations on the string. OP is already converted the string to a raw string r"path", so escape characters wasn't the problem.
– DJK
                Aug 9, 2017 at 16:48
                I have the error FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users...\\xxx.csv' ' , my code is using os.rename(r'C:Users\...\\xxxx.csv', r'C:\Users\...\xxxx_' + str(current_date) + '.csv') How can I solve it?
– wawawa
                Apr 14, 2020 at 16:12
                I had a similar issue in Jupyter with only some newly added sub-directories.  driving me crazy, but adding "r" before the quoted path using os.chdir fixed it.  Does r somehow tell the program that it is a path and not an escape character?
– wiseass
                Apr 29, 2020 at 13:09
                I had a similar issue with Python3 running on Windows 10. The problem was, I had one line in my .py file which had     os.makedirs('.\model') that worked, but another line     os.makedirs('.\tokenizer') which gave the error as in the title of this issue.  It turns out \t is a special literal (tab) and is interpreted differently and hence gave this error.  Solution is to use \\ everywhere as this solution suggests.  The following worked.    os.makedirs('.\\model') and os.makedirs('.\\tokenizer')
– sri_s
                May 26 at 4:46

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

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

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.
  • 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.

    I was trying to get the file count by sending path through a .robot file to one file count function which was a pyton file, but was getting the same error message. Below is the code

    import os, os.path
    count = 0
    def get_file_count(desired_path):
       for path in os.listdir(desired_path):
          if os.path.isfile(os.path.join(desired_path, path)):
             count += 1   
       print('File Count:', count)
       return count
    

    And the robot file was like

    ${download_dir}=     Join Path   C:/Users/user/Downloads
    Log     ${download_dir}
    ${path}=    Catenate    SEPARATOR=    r    "${download_dir}"
    ${count}=    file_count.Get File Count    ${path}
    Log    ${count}
    

    The ${path} was storing the value as 'r"C:/Users/user/Downloads" but it was failing with above error message. The solution I got for this was

    import os, os.path
    def get_file_count(desired_path):
       count = 0
       print(desired_path)
       for path in os.listdir(desired_path):
           if os.path.isfile(os.path.join(desired_path, path)):
               count += 1            
       print('File Count:', count)
       return count
    

    Updated Robot file:

     *** Variables ***
     ${path}    C:\Users\user\Downloads
     *** Test Cases ***  
        ${count}=    file_count.Get File Count    ${path}    
        Log    ${count}
    

    Just updated the path in robot file and moved count=0 inside the loop and it was working good for me.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.