繁体   English   中英

PermissionError: [WinError 32] 该进程无法访问该文件,因为它正被另一个进程使用:(打开 Excel 文件) in Python

[英]PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: (Open Excel File) in Python

我需要帮助解决我遇到的问题。 “PermissionError:[WinError 32] 该进程无法访问该文件,因为它正被另一个进程使用:”。

因此,下面的脚本删除了一个包含 excel 文件的文件夹。 但是,如果打开 excel 文件,它不会继续执行 shutil.rmtree(dirpath) 命令。 有人可以引导我找到打开文件时提示用户的解决方案吗? 期待您的反馈。 非常感谢你提前。

import os
import shutil

dirpath = os.path.join('C:/Path/Folder', 'Folder')
if os.path.exists(dirpath) and os.path.isdir(dirpath):
   shutil.rmtree(dirpath)
   print('Deleted.')

else:
   print('Folder does not exist!')
   messagebox.showinfo('Ok.')

鉴于您使用的是 Windows,我会说尝试以下操作:

import os
import shutil

dirpath = os.path.join('C:/Path/Folder', 'Folder')
if os.path.exists(dirpath) and os.path.isdir(dirpath):
   try:
       os.rename(dirpath, dirpath)
       shutil.rmtree(dirpath)
       print('Deleted.')
   except:
       messagebox.showinfo('File opened by another process')

else:
   print('Folder does not exist!')
   messagebox.showinfo('Ok.')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM