pythonshutilfilenotfounderror

How do I fix this simple FileNotFound error in Python?


I'm making a simple code with the shutil module of copying another contents of a file and had a FileNotFound error, which I want to use the file name instead of pasting the file path every single time. But no matter what I do, I would always need to paste the file path to not get the error.

  1. I checked if the file names didn't match but they did,
  2. I tried another file with a .txt and matched it carefully it STILL gave me an error.
  3. I tried using the python files and insert my folder there through C:\Users\user\AppData\Local\Programs\Python\Python312 but the same error occurs
  4. I supposed Vscode can't reach my file since I thought it didn't have authorization so I went to C:\Users\user\AppData\Local\Programs\Microsoft VS Code then inide Microsoft VS Code I moved the file, ran it but the same error appears.
  5. I supposed I need to select the Microsoft VS Code folder instead, rather than the actual folder I store my projects. So I ran it but still, the same error appears. I tried this with the python folders but the still same error.
  6. I used a different path instead C:\Users\user.vscode with 2 tries with selecting the folder I store my projects and the folder of .vscode but both didn't work I would always need to tediously paste the file path every single time
import shutil

shutil.copyfile("test",'copy.txt') #scr,dst

Error:

Traceback (most recent call last):
  File "c:\Users\user\AppData\Local\Programs\Microsoft VS Code\My Projo's (VSCODE)\Python 🐍\Python 🐍 - Lessons\DUPEME", line 9, in <module>
    shutil.copyfile("test", "copy.txt")
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\shutil.py", line 260, in copyfile
    with open(src, 'rb') as fsrc:
         ^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'test'

Solution

  • There is no issue with the code, the issue is where you are running the program from, and the existence of the src file. The error "No such file or directory: 'test'" means that python is searching for a file called "test" inside of the current working directory, but is not finding it. This code will work fine from anywhere you run it on the computer, as long as both the "test" file and "copy.txt" files exist in the working directory. It is good practice to dedicate an entire new directory to each project and then place the python script inside it, along with the "test" file and the "copy.txt" file. If all 3 exist and are in the same location and you run the script it will work.

    Steps to fix your error:

    1. Create a new directory at a desired location ("C:\Users\user\project")
    2. Create the main.py file in the root of the directory and copy the script into the new file.
    3. Create a new file called "test" in the root of the directory. The contents of this file is what will be copied to the destination file.
    4. Create a new file called "copy.txt" also in the root of the directory.
    5. You can now run the program from the terminal using python3 main.py, just make sure the current working directory of the terminal process is the root of the project. You can ensure this by running 'cd C:\Users\user\project'

    If you want to change the project structure to allow for the script to be run from anywhere, you can use absolute paths. So let's say the python file is located at D:\scripts and the "test" and "copy.txt" files are located at C:\Users\user\files you can replace the paths in the python code with their absolute paths: shutil.copyfile("C:\Users\user\files\test", C:\Users\user\files\copy.txt"). Then the python script will work even though it's in a completely separate directory.