gpt4 book ai didi

python - 递归删除目录和所有符号链接(symbolic link)

转载 作者:行者123 更新时间:2023-12-04 17:07:31 26 4
gpt4 key购买 nike

bounty明天到期。回答这个问题有资格获得 +50 声望奖励。
Jürgen K.想提请大家注意这个问题:

recursive deletion of all files, symbol links included








我尝试使用 shutil删除目录和所有包含的文件,如下所示:
import shutil
from os.path import exists
if exists(path_dir):
shutil.rmtree(path_dir)
不幸的是,我的解决方案不起作用,抛出以下错误:
FileNotFoundError: [Errno 2] No such file or directory: '._image1.jpg'
快速搜索显示我是 not alone在遇到这个问题。
据我了解, rmtree功能相当于 rm -Rf $DIR shell 命令 - 但情况似乎并非如此。
附言用于重建目的。请创建一个符号链接(symbolic link),例如使用 ln -s /path/to/original /path/to/link

最佳答案

这很奇怪,在 Windows 10 和 Ubuntu 20.04.2 LTS 中,我对要删除的文件夹下有或没有符号链接(symbolic link)的 Shutil.rmtree() 没有任何问题。
无论如何尝试以下代码。我在 Windows 10 和 Ubuntu 中尝试过。

from pathlib import Path
import shutil


def delete_dir_recursion(p):
"""
Delete folder, sub-folders and files.
"""
for f in p.glob('**/*'):
if f.is_symlink():
f.unlink(missing_ok=True) # missing_ok is added in python 3.8
print(f'symlink {f.name} from path {f} was deleted')
elif f.is_file():
f.unlink()
print(f'file: {f.name} from path {f} was deleted')
elif f.is_dir():
try:
f.rmdir() # delete empty sub-folder
print(f'folder: {f.name} from path {f} was deleted')
except OSError: # sub-folder is not empty
delete_dir_recursion(f) # recurse the current sub-folder
except Exception as exception: # capture other exception
print(f'exception name: {exception.__class__.__name__}')
print(f'exception msg: {exception}')

try:
p.rmdir() # time to delete an empty folder
print(f'folder: {p.name} from path {p} was deleted')
except NotADirectoryError:
p.unlink() # delete folder even if it is a symlink, linux
print(f'symlink folder: {p.name} from path {p} was deleted')
except Exception as exception:
print(f'exception name: {exception.__class__.__name__}')
print(f'exception msg: {exception}')


def delete_dir(folder):
p = Path(folder)

if not p.exists():
print(f'The path {p} does not exists!')
return

# Attempt to delete the whole folder at once.
try:
shutil.rmtree(p)
except Exception as exception:
print(f'exception name: {exception.__class__.__name__}')
print(f'exception msg: {exception}')
# continue parsing the folder
else: # else if no issues on rmtree()
if not p.exists(): # verify
print(f'folder {p} was successfully deleted by shutil.rmtree!')
return

print(f'Parse the folder {folder} ...')
delete_dir_recursion(p)

if not p.exists(): # verify
print(f'folder {p} was successfully deleted!')

# start
folder_to_delete = '/home/zz/tmp/sample/b' # delete folder b
delete_dir(folder_to_delete)
示例输出:
我们要删除文件夹 b .
.
├── 1.txt
├── a
├── b
│   ├── 1
│   ├── 1.txt -> ../1.txt
│   ├── 2
│   │   └── 21
│   │   └── 21.txt
│   ├── 3
│   │   └── 31
│   ├── 4
│   │   └── c -> ../../c
│   ├── a -> ../a
│   └── b.txt
├── c

Parse the folder /home/zz/tmp/sample/b ...
symlink a from path /home/zz/tmp/sample/b/a was deleted
symlink c from path /home/zz/tmp/sample/b/4/c was deleted
folder: 4 from path /home/zz/tmp/sample/b/4 was deleted
symlink 1.txt from path /home/zz/tmp/sample/b/1.txt was deleted
file: b.txt from path /home/zz/tmp/sample/b/b.txt was deleted
file: 21.txt from path /home/zz/tmp/sample/b/2/21/21.txt was deleted
folder: 21 from path /home/zz/tmp/sample/b/2/21 was deleted
folder: 2 from path /home/zz/tmp/sample/b/2 was deleted
folder: 1 from path /home/zz/tmp/sample/b/1 was deleted
folder: 31 from path /home/zz/tmp/sample/b/3/31 was deleted
folder: 3 from path /home/zz/tmp/sample/b/3 was deleted
folder: b from path /home/zz/tmp/sample/b was deleted
folder /home/zz/tmp/sample/b was successfully deleted!

关于python - 递归删除目录和所有符号链接(symbolic link),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70246591/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com