'FileNotFoundError, when I run shutil.copy, 1 particular ".dylib" file throws FileNotFoundError error every time

I am making a python app and one of it's functions involves copying the contents of one directory to several different locations. Some of these files are copied to the same directory in which they currently reside and are renamed. It almost always works except when it hit this one particular file.

This is the code that does the copying.

shutil.copy(original_path_and_file,os.path.join(redun.path, redun.file_name))

The particular copy that causes the crash is when it's trying to copy /Users/<myusername>/Desktop/archive1/test_2_here/myproject/liboqs.dylib to /Users/<myusername>/Desktop/archive1/test_2_here/myproject/liboqs.0.dylib

This is the error that appears in my terminal:

FileNotFoundError: [Errno 2] No such file or directory: '/Users/<myusername>/Desktop/archive1/test_2_here/myproject/liboqs.dylib'  

Why does this file throw the error and none of the other files that it copies throw any errors?

Update

The file is actually an "Alias" according to finder:
enter image description here



Solution 1:[1]

Perhaps you could attempt to solve it by connecting the directory path and the filename like follows:

One problem here is that you do not specify the path of the file. As you are executing the command from the parent directory, the script has no way of knowing that testfile2.txt is in a subdirectory of your input directory. To fix this, use:

shutil.copy(os.path.join(foldername, filename), copyDirAbs)

or

# Recursively walk the Search Directory, copying matching files
# to the Copy Directory
for foldername, subfolders, filenames in os.walk(searchDirAbs):
    print('Searching files in %s...' % (foldername))
    for filename in filenames:
        if filename.endswith('.%s' % extension):
            print('Copying ' + filename)
            print('Copying to ' + copyDirAbs)
            totalCopyPath = os.path.join(searchDirAbs, filename)
            shutil.copy(totalCopyPath, copyDirAbs)

print('Done.')

The Python FileNotFoundError: [Errno 2] No such file or directory error is often raised by the os library. This error tells you that you are trying to access a file or folder that does not exist. To fix this error, check that you are referring to the right file or folder in your program.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tharun P