Python复制MP3文件遇PermissionError[Errno13],求解决建议
Hey there, sorry you're stuck with this permission error even after running as admin—let's walk through some fixes that should get your MP3 copy script working smoothly.
常见原因与解决方案
1. 目标目录受系统保护
Even admin accounts can't write to certain system directories (like C:\Windows or Program Files) without extra restrictions. Try these quick fixes:
- Switch your target directory to a user-owned folder, like
C:\Users\YourName\Music\CopiedMP3s—these directories have looser permissions by default. - Double-check that your target path in code doesn't have typos or special characters (like em dashes or spaces without proper escaping) that might trigger unexpected permission blocks.
2. 源MP3文件被其他程序占用
If any of your MP3s are open in a media player, file explorer preview pane, or another app, Python can't read/copy them.
- Close all apps that might be accessing the files, then rerun the script.
- Add a retry mechanism in your code to handle temporary locks:
import shutil from pathlib import Path import time def copy_with_retry(src, dest, retries=3, delay=2): for attempt in range(retries): try: shutil.copy2(src, dest) return True except PermissionError: time.sleep(delay) return False source_dir = Path("your/source/directory") target_dir = Path("your/target/directory") # 自动创建目标目录(含父目录),避免因目录不存在报错 target_dir.mkdir(parents=True, exist_ok=True) for mp3_file in source_dir.rglob("*.mp3"): if copy_with_retry(mp3_file, target_dir / mp3_file.name): print(f"复制成功: {mp3_file.name}") else: print(f"尝试3次后仍无法复制: {mp3_file.name}")
3. 路径处理不规范
Hardcoding paths with spaces or backslashes can lead to hidden issues. Use Python's pathlib module for safer path handling—it avoids manual string concatenation errors. For Windows paths, either use raw strings (like r"C:\My Folder") or forward slashes ("C:/My Folder").
4. UAC权限未完全继承
Sometimes opening an admin cmd isn't enough—your Python process might not fully inherit admin rights. Try:
- Right-click your Python script file directly and select Run as administrator.
- If you're using a virtual environment, activate it inside the admin cmd before running the script.
5. 手动修复文件夹权限
If the target directory has broken permission inheritance:
- Right-click the target folder → Properties → Security tab.
- Click Edit → Add, type your admin username, then click Check Names to confirm.
- Select your admin account, check the Full Control box under Allow, then click Apply and OK.
内容的提问来源于stack exchange,提问作者Martin Preradović

