是否可以删除文件夹中的所有文件夹而不使用特定路径?,在这里我移动文件的内容,然后我想删除,如果它是一个目录
import os, zipfile
import shutil
import os
from os import path
dir_name = 'C:\\Users\\Guest\\Desktop\\OJT\\samples'
destination = 'C:\\Users\\Guest\\Desktop\\OJT\\scanner\\test'
for path, subdirs, files in os.walk(destination):
for name in files:
filename = os.path.join(path, name)
shutil.copy2(filename, destination)
正如前面@Vineeth Sai所建议的,如果要删除目录中的所有子目录,只需使用^{} 遍历每个文件,如果文件是目录,则应用^{} :
上面还使用^{} 检查文件是否是目录。你知道吗
是的,使用shutil的
rmtree
方法。你知道吗您也可以使用
os.rmdir
,但如果其中包含任何内容,则这将不起作用。你知道吗如果要检查特定路径是否为目录,则可以使用
os.path.isdir
,然后运行rmtree
,如果返回TRUE
如果您想保持根文件夹完好无损,那么可以
walk
该目录并对每个项调用rmtree
。你知道吗如果
Vineeth's
答案不适合您的情况,您可以使用subprocess
模块运行os
特定的命令,如下所示上面的命令是
linux
特定的,您可以使用上面相同命令的windows对应项。你知道吗注意-这里
shell=True
将*
展开到文件/文件夹中。你知道吗另外,注意
Vineeth's
答案是os
独立的,上面的答案是os
特定的。小心。你知道吗另外,您还可以使用
subprocess
模块运行powershell
命令。你知道吗相关问题 更多 >
编程相关推荐