FileNotFoundError: (WinError 2) the system cannot find the file specified

Home/ Questions/filenotfounderror: [winerror 2] the system cannot find the file specified. How to fix this error?

FileNotFoundError: (WinError 2) the system cannot find the file specified

  • 3

  • 3

Please help me solve the filenotfounderror: [winerror 2] the system cannot find the file specified problem. 

Here is my entire error message:

>>> import subprocess >>> subprocess.run(["dir", "/p"]) Traceback (most recent call last): File "", line 1, in File "C:\Users\zhaosong\anaconda3\envs\env_python_37\lib\subprocess.py", line 488, in run with Popen(*popenargs, **kwargs) as process: File "C:\Users\zhaosong\anaconda3\envs\env_python_37\lib\subprocess.py", line 800, in __init__ restore_signals, start_new_session) File "C:\Users\zhaosong\anaconda3\envs\env_python_37\lib\subprocess.py", line 1207, in _execute_child startupinfo) FileNotFoundError: [WinError 2] The system cannot find the file specified

Hope to receive an answer soon about the cause of the error and how to handle it. I really appreciate everyone’s help. Thank.

  • Report

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Pick a usernameEmail AddressPassword

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python – FileNotFoundError: [WinError 2] The system cannot find the file specified:

As written youre looking for a file named 0.jpg in the working directory. You want to be looking in the directory you pass in.

So instead do:

os.rename(os.path.join(directory,name), os.path.join(directory,0+name))

You cannot use absolute path unless your terminal is in that directory.
Hence you can do as following:

import os def rename(directory): os.chdir(directory) # Changing to the directory you specified. for name in os.listdir(directory): print(name) os.rename(name,0+name)

python – FileNotFoundError: [WinError 2] The system cannot find the file specified:

Agreeing with Bernies answer that filename is used to mean the full/absolute path name. The below will also work.

os.rename((directory+name),(directory+0+name))

Related posts on python :

  • python – IsADirectoryError: [Errno 21] Is a directory: It is a file
  • python – s3 urls – get bucket name and path
  • How can I tail a log file in Python?
  • python – Create dynamic URLs in Flask with url_for()
  • python – Usage of sys.stdout.flush() method
  • Python Error: AttributeError: __enter__
  • OSError [Errno 22] invalid argument when use open() in Python
  • python – TypeError: string argument without an encoding

A lot of programmers have been getting the “filenotfounderror: [winerror 2] the system cannot find the file specified” message and this is a common programming error that can be caused due to a wide variety of different reasons. In this article, we have dedicated ourselves to fixing this issue.

FileNotFoundError: (WinError 2) the system cannot find the file specified
filenotfounderror: [winerror 2] the system cannot find the file specified

After going through a lot of different user feedback-based messages, we concluded that the following must be the primary reasons due to which this error occurs.

  • Coding Faults: There exist a variety of different faults that might be present in your code that can cause the “filenotfounderror: [winerror 2] the system cannot find the file specified” error and if not fixed timely, you will run into this error message. Therefore, we recommend that you try and straighten out your code or ask a question on Github regarding it where programmers could troubleshoot it and help you in running it more efficiently.
  • Admin Permissions: In some cases, the admin permissions might be missing and due to this, the IDE might be unable to properly execute the code. Therefore, we recommend that after verifying the syntax of your code, you run it with admin permissions to fix the “filenotfounderror: [winerror 2] the system cannot find the file specified” error.
  • Antivirus Prevention: The antivirus installed on your computer might be preventing the code from being executed by not letting it through the operating system’s firewall. Therefore, we insist that you disable the Antivirus temporarily and then check to see if doing so fixes the issue.

Now that you have been made familiar with most of the reasons, we can finally start implementing the fixes.

Fixing the FileNotFoundError: [WinError 2] The system cannot find the file specified issue:

Solution 1: Change the Order Of Slashes

Sometimes the use of incorrect slashes can also trigger this error. So try converting your “Forward Slashes /” into “Backward Slashes \” and vice versa and afterward proceed to check if this fixes your error.

Solution 2: Try Setting Up Your Environment

  1. On your PC open up the “Anaconda Prompt” and activate your environment (optional for non-env users).
  2. Then type in the following command: python –m ipykernel install –user
  3. Now launch your jupyter notebook and check if it works fine for you.

Solution 3: Change Your Environment Variables

Follow the steps below to change your environment variables:

  1. In the search bar near the Start menu search for “Edit the System Environment Variables” and choose to select it from the search results.
    FileNotFoundError: (WinError 2) the system cannot find the file specified
    Edit the system Environmental Variables
  2. In the “Advanced” window proceed to select “Environment Variables” at the bottom.
    FileNotFoundError: (WinError 2) the system cannot find the file specified
    Choose Environmental Variables Option
  3. Here click on the “Property” that you would like to change and then click on “Edit”.
  4. If the property is a list of files or directories, then click on the “New” button and add the variable name and value in the new system variable window.
  5. In the end, click on “OK” to save all the changes made.

Solution 4: Reconfigure the argv Python Path

A few bunches of users reported that this solution also proved to be effective in their case.

  1. Open your desktop PC and proceed to the following directory:
/python/share/jupyter/kernels/
  1. Then open the “kernel.json” and here set the “argv” python path as below:
{ "argv": [ "C:\Anaconda3\python.exe", "-m", "ipykernel_launcher", "-f", "{connection_file}" ], "display_name": "Python 3", "language": "python" }

3. Lastly, launch the jupyter notebook and check if the python subprocess filenotfounderror: [winerror 2] the system cannot find the file specified error got resolved.

Solution 5: Run Python Using CMD

If the thing you’re trying to run is not being set up in the system path then the error can occur too. In order to work with Sublime, you need to be able to open a cmd window and enter the following command:

Terence>python

If you are trying to run python then you should be able to see the following message after the execution of this command:

C:\Users\Terence>python Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AM D64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>

Else, if you get the following message then this means Windows can’t execute the program as it doesn’t know where it is:

C:\Users\Terence>python 'python' is not recognized as an internal or external command, operable program or batch file.

Solution 6: Reconfigure Shell=True Argument

If you do not specify the “Shell=True” argument in the sub-process module’s run method then you may end up having this error. To solve this error add the “Shell=True” argument in the sub-process module’s run or you can also change “Shell=False” to “Shell=True” and then see if you get rid of the error.

Solution 7: Run the Script As An Administrator

  1. Create a shortcut for python.exe by right-clicking on “python.exe” and then selecting “Create A Shortcut” from the drop-down menu.
  2. Once the shortcut is created, right-click the shortcut and select “Properties” from the drop-down menu.
  3. Now in the new pop up window change the “Target” to something like below:
C:\xxx\...\python.exe your_script.py
  1. Once done, click on “Advanced” in the “Shortcut” tab and then checkmark the box for “Run As Administrator”.
  2. Then apply all the changes made and click on “OK” to save the changes.

Solution 8: Disable Anti-Virus

Lastly, you may want to confirm if Windows isn’t blocking access to the folder you are trying to make a virtual environment in. So you can temporarily disable your Windows Security features to see if it helps. To do so follow these steps:

  1. In the search bar near the Start menu search for “Windows Security” and click to select it from the list of search results.
  2. In the pop-up Windows Security window click to select “Virus and Threat Protection”.
    FileNotFoundError: (WinError 2) the system cannot find the file specified
    Select Virus and threat protection
  3. Now proceed to select “Manage Settings” under the “Virus and Threat Protection Settings” section.
  4. In here turn “OFF” the “Real-Time Protection” using the slide button and afterward proceed to check if the error got solved.
    FileNotFoundError: (WinError 2) the system cannot find the file specified
    Disable Real-time protection

Hopefully, careful implementation of the above-indexed fixes will lead you to get rid of the error eventually but if the error still exists then feel free to Contact Us here and we will further troubleshoot it for you.