Python [WinError 3] The system cannot find the path specified

Python error [WinError 3] is a variation of the [WinError 2] error. The complete error message is as follows:

FileNotFoundError: [WinError 3] The system cannot find the path specified

This error usually occurs when you use the Python os module to interact with the Windows filesystem.

While [WinError 2] means that a file can’t be found, [WinError 3] means that the path you specified doesn’t exist.

This article will show you several examples that can cause this error and how to fix it.

1. Typed the wrong name when calling os.listdir() method

Suppose you have the following directory structure on your computer:

.
├── assets
│   ├── image.png
│   └── photo.png
└── main.py

Next, suppose you try to get the names of the files inside the assets/ directory using the os.listdir() method.

But you specified the wrong directory name when calling the method as follows:

import os

files = os.listdir("asset")
print(files)

Because you have an assets folder and not asset, the os module can’t find the directory:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    files = os.listdir("asset")
            ^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'asset'

To fix this error, make sure that the path you passed as the parameter to the listdir() method exists.

For our example, replacing asset with assets should fix the error.

2. Specifying non-existent path in os.rename() method.

The error also appears when you specified the wrong path name when calling the os.rename() method.

Suppose you have a different directory structure on your computer as follows:

.
├── docs
│   └── file.txt  
└── main.py

Now, you want to rename the file.txt file into article.txt file.

You called the os.rename() method as follows:

import os

os.rename("doc/file.txt", "article.txt")

The code above incorrectly specified the path docs/ as doc/, so the error is triggered:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    os.rename("doc/file.txt", "article.txt")
FileNotFoundError: [WinError 3] The system cannot find the path specified: 
'doc/file.txt' -> 'article.txt'

To fix this error, you need to specify the correct path to the file, which is docs/file.txt.

Please note that the extension of the file must be specified in the arguments. If you type file.txt as file, then you’ll get the same error.

This is because Python will think you’re instructing to rename a directory or folder and not a file.

When renaming a file, always include the file extension.

Also, keep in mind that directory and file names are case-sensitive, so you need to use the right capitalization.

3. Use absolute path instead of relative

At times, you might want to access a folder or file that’s a bit difficult to reach from the location of your script.

Suppose you have a directory structure as follows on your computer:

. C:
├── assets
│   └── text
│       └── file.txt
└── scripts
    └── test
        └──  main.py

In this structure, the path to main.py is C:/scripts/test/main.py, while the file.txt is in C:/assets/text/file.txt

Suppose you want to rename file.txt to article.txt, this is how you specify the name with relative paths:

import os

os.rename("../../assets/text/file.txt", "../../assets/text/article.txt")

It’s easy for you to specify the wrong path when using relative paths, so it’s recommended to use absolute paths when the path is complex.

Here’s what the arguments look like using absolute paths:

import os

os.rename("C:/assets/text/file.txt", "C:/assets/text/article.txt")

As you can see, absolute paths are easier to read and understand. In Windows, the absolute path usually starts with the drive letter you have in your system like C: or D:.

To find the absolute path of a file, right-click on the file and select Properties from the context menu.

You’ll see the location of the file as follows:

Add the file name at the end of the location path, and you get the absolute path.

I hope this tutorial is helpful. See you in other articles! 👋

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.