Home > OS >  PermissionError when copying files, renaming them and saving to a new location
PermissionError when copying files, renaming them and saving to a new location

Time:12-17

enter image description hereHey guys! The file has all premisions and i cant fidure out what is the problem. I did try both shutil.copyfile and shutil.copy

ples help

Hey guys! The file has all premisions and i cant fidure out what is the problem. I did try both shutil.copyfile and shutil.copy

ples help

CodePudding user response:

It seems like you are trying to copy a file using the shutil module in Python, but you are encountering an error. Without more information about the error message you are receiving and the code you are using, it is difficult to pinpoint the cause of the problem.

Here are a few possible issues that could be causing the error, along with some suggestions for how to troubleshoot them:

The source file or destination directory does not exist: Make sure that the source file and the destination directory both exist and are accessible. You can use the os.path.exists() function to check if a file or directory exists, and the os.access() function to check if you have the necessary permissions to access it.

The source file or destination directory is a directory, not a file: The shutil.copyfile() function only works for copying individual files, not directories. If you are trying to copy a directory, you can use the shutil.copytree() function instead.

There is a file with the same name as the destination file in the destination directory: If there is already a file with the same name as the destination file in the destination directory, the shutil.copyfile() function will overwrite it. If you want to preserve the existing file, you can either choose a different name for the destination file or use the shutil.copy() function instead, which will automatically rename the destination file if it already exists.

CodePudding user response:

original_file_path seems not to be a file path, but a path to a directory. The error doesn't make it too clear, but shutil.copyfile() only works on files and will throw a PermissionError. Try shutil.copytree(); from the docs:

Recursively copy an entire directory tree rooted at src to a directory named dst and return the destination directory

  • Related