shutil.copy 的权限被拒绝

问题描述 投票:0回答:1

为什么当我运行这个 python 代码时(即使是作为管理员,是的,我已经事先检查了权限),它一直告诉我“权限被拒绝”?有人可以帮我解决这个问题吗?

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}")

如前所述,我尝试了检查权限、以管理员身份运行等,但仍然不起作用。

请帮忙。

python permission-denied shutil
1个回答
0
投票

尝试不使用python来移动或复制文件,只需使用win10或linux,

然后看看移动或复制文件后会发生什么。

© www.soinside.com 2019 - 2024. All rights reserved.