【问题标题】:PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: (Open Excel File) in PythonPermissionError:[WinError 32] 该进程无法访问该文件,因为它正在被另一个进程使用:(打开 Excel 文件)在 Python 中
【发布时间】:2023-01-14 13:47:43
【问题描述】:

我需要帮助解决我遇到的问题。 “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.')

【问题讨论】:

    标签: python excel shutil


    【解决方案1】:

    鉴于您使用的是 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.')
    

    【讨论】:

      猜你喜欢
      • 2019-11-26
      • 2015-11-13
      • 2015-01-28
      • 1970-01-01
      • 2017-06-02
      • 2021-09-21
      • 2020-10-31
      • 2018-10-28
      • 1970-01-01
      相关资源
      最近更新 更多