Python代码执行shutil操作遇Permission Denied错误求助
Permission Denied 错误排查(Python文件移动/复制)
运行以下Python代码时,即便以管理员身份运行且提前检查了权限,仍持续收到“Permission Denied”错误,请求排查解决:
import os import shutil import tkinter as tk from tkinter import filedialog import zipfile root = tk.Tk() root.withdraw() starting_zip_file = filedialog.askopenfilename() if starting_zip_file: print(f"Selected file: {starting_zip_file}") else: print("No file selected") exit(0) destination_folder = r'C:\Users\user\Downloads\folder1' copy_folder = r'C:\Users\user\Downloads\folder2' parent_folder1 = destination_folder new_folder_name = input("Enter the name of the new folder: ") destination_path = os.path.join(parent_folder1, new_folder_name) if not os.path.exists(destination_path): os.mkdir(destination_path) print(f"Folder '{new_folder_name}' created in '{parent_folder1}'.") else: print(f"Folder '{new_folder_name}' already exists in '{parent_folder1}'.") parent_folder2 = copy_folder temp_folder = 'TEMP' temp_folder_path = os.path.join(parent_folder2, temp_folder) if not os.path.exists(temp_folder_path): os.mkdir(temp_folder_path) print(f"Folder 'TEMP' created in '{parent_folder2}'.") else: print(f"Folder 'TEMP' already exists in '{parent_folder2}'.") try: shutil.move(starting_zip_file, destination_path) print(f"File '{starting_zip_file}' moved") except FileNotFoundError: print(f"File '{starting_zip_file}' not found in the source folder.") except PermissionError: print(f"Permission denied for file '{starting_zip_file}'. Check permissions.") except Exception as e: print(f"An error occurred while moving file '{starting_zip_file}': {e}") try: shutil.copyfile(destination_path, temp_folder_path) print(f"File '{starting_zip_file}' copied to 'TEMP' folder.") except FileNotFoundError: print(f"File '{starting_zip_file}' not found in the destination folder.") except PermissionError: print(f"Permission denied for file '{starting_zip_file}'. Check permissions.") except Exception as e: print(f"An error occurred while copying file '{starting_zip_file}': {e}")
问题排查与修复方案
文件被其他进程占用
选中的ZIP文件可能正被资源管理器预览、解压软件等程序占用,导致无法修改。关闭所有可能访问该文件的程序后重试,也可以借助psutil模块检测占用进程:import psutil def is_file_locked(file_path): for proc in psutil.process_iter(['pid', 'name', 'open_files']): try: for item in proc.open_files(): if file_path == item.path: return True, proc.name except (psutil.NoSuchProcess, psutil.AccessDenied): continue return False, None locked, proc_name = is_file_locked(starting_zip_file) if locked: print(f"File is locked by process: {proc_name}")shutil.copyfile参数错误
代码中shutil.copyfile(destination_path, temp_folder_path)存在逻辑错误:destination_path是文件夹路径,但shutil.copyfile要求源和目标必须是完整文件路径,不能是文件夹。修改复制逻辑:try: # 获取原文件名,拼接目标文件的完整路径 file_name = os.path.basename(starting_zip_file) source_copy_path = os.path.join(destination_path, file_name) target_copy_path = os.path.join(temp_folder_path, file_name) shutil.copyfile(source_copy_path, target_copy_path) print(f"File '{starting_zip_file}' copied to 'TEMP' folder.") except FileNotFoundError: print(f"File '{source_copy_path}' not found in the destination folder.") except PermissionError: print(f"Permission denied when copying file '{source_copy_path}'.") except Exception as e: print(f"An error occurred while copying file: {e}")NTFS权限或UAC限制
即使以管理员身份运行,Windows的NTFS权限可能限制了写入操作。右键目标文件夹(folder1、folder2)→属性→安全→检查当前用户是否拥有写入/完全控制权限,若没有则添加对应权限。目标文件夹异常
若TEMP文件夹或新建的目标文件夹被标记为只读,或被系统锁定,尝试删除现有文件夹后重新创建,或更换其他测试路径(比如D:\test)验证是否路径本身存在问题。
修改后的完整代码
import os import shutil import tkinter as tk from tkinter import filedialog import zipfile root = tk.Tk() root.withdraw() starting_zip_file = filedialog.askopenfilename() if starting_zip_file: print(f"Selected file: {starting_zip_file}") else: print("No file selected") exit(0) destination_folder = r'C:\Users\user\Downloads\folder1' copy_folder = r'C:\Users\user\Downloads\folder2' parent_folder1 = destination_folder new_folder_name = input("Enter the name of the new folder: ") destination_path = os.path.join(parent_folder1, new_folder_name) if not os.path.exists(destination_path): os.mkdir(destination_path) print(f"Folder '{new_folder_name}' created in '{parent_folder1}'.") else: print(f"Folder '{new_folder_name}' already exists in '{parent_folder1}'.") parent_folder2 = copy_folder temp_folder = 'TEMP' temp_folder_path = os.path.join(parent_folder2, temp_folder) if not os.path.exists(temp_folder_path): os.mkdir(temp_folder_path) print(f"Folder 'TEMP' created in '{parent_folder2}'.") else: print(f"Folder 'TEMP' already exists in '{parent_folder2}'.") # 移动文件逻辑 try: shutil.move(starting_zip_file, destination_path) print(f"File '{starting_zip_file}' moved to '{destination_path}'") except FileNotFoundError: print(f"File '{starting_zip_file}' not found in the source folder.") except PermissionError: print(f"Permission denied for file '{starting_zip_file}'. Check if the file is in use.") except Exception as e: print(f"An error occurred while moving file: {e}") # 修复后的复制文件逻辑 try: file_name = os.path.basename(starting_zip_file) source_copy_path = os.path.join(destination_path, file_name) target_copy_path = os.path.join(temp_folder_path, file_name) shutil.copyfile(source_copy_path, target_copy_path) print(f"File '{file_name}' copied to '{temp_folder_path}'") except FileNotFoundError: print(f"File '{source_copy_path}' not found in the destination folder.") except PermissionError: print(f"Permission denied when copying file '{source_copy_path}'. Check folder permissions.") except Exception as e: print(f"An error occurred while copying file: {e}")
内容的提问来源于stack exchange,提问作者user22538038
相关产品推荐
相关产品推荐

