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:
CodePudding user response:
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.
CodePudding user response:
The way I fixed this was by having the app ignore link files. I wrote this:
if not os.path.islink(original_path_and_file):
shutil.copy(original_path_and_file,os.path.join(redun.path, redun.file_name))
This is technically more of a work-around than a fix because it doesn't actually copy the file but I decided the app still works fine when not copying link files.