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

Today in this article, we will cover below aspects,

Issue Description

Python runtime gives below error,

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

Python WindowsError Error 123 The filename directory name

We shall try to solve the Python WindowsError: [Error 123] using some simple best pratices.

Resolution

This issue surfaced while performing read and write to file using python code.

I had my file located in the location below,

C:\Test\file\file-input-thecodebuzz.txt

When using the path in the python code, please follow below,

  • Use ‘r’ before any path- The r  is a string literal that it let treat any string as a raw string, which means all escape codes will be ignored.
  • Use either double quotes “file-path” or single quote ‘file-path’ to specify the path.
  • Don’t use a combination of both “” or ‘

Correct file path is as below,

filepath = r'C:\Test\file\file-input-thecodebuzz.txt'

OR

filepath = r"C:\Test\file\file-input-thecodebuzz.txt"


What is ‘r’ in Python

The r is a string literal that let’s treat any string as a raw string, which means all escape codes will be ignored.

  • Without the r, backslashes are treated as escape characters.
  • With the r, backslashes are treated as literal.

For more details please see here

With the above measures , I was able to solved the problem I had.

References:

Did I miss anything else in these resolution steps?

Did the above steps resolve your issue? Please sound off your comments below!

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *