lzhongs 2015-03-27 03:35 采纳率: 50%
浏览 1788

Pathon 删除指定目录下的文件, 我是coding online,无法调试,代码报错,具体如下:

#Delete all files under the pointed path

import os

filePath = raw_input("Input path where you want delete:\n")

#If file path is null, we should initial the variable "/home"
if filePath == "":
filePath = "/home"

#Obtain the path or file under the filePath and restore in list named paths
def obtainPath(path):
try:
print path
paths = os.listdir(path)
except:
print "Obtain the path or file occured an error!"
return paths

#Inith list paths and root path
initialPaths = obtainPath(filePath)
initialRoot = filePath

#Do loop to search file and then delete
def loopDelete(initialPaths, initialRoot):
while len(initialPaths) > 0:
for temp in initialPaths:
tempPath = initialRoot + '/' + temp
initialPaths.remove(temp)

        if os.path.isfile(tempPath):
            os.remove(tempPath)
        else:
            loopDelete(obtainPath(tempPath), tempPath)

#Invoke the loop delete way to excute
loopDelete(initialPaths, initialRoot)

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-03-27 03:49
    关注

    如果代码不是你写的,首先检查权限问题。

    评论

报告相同问题?

悬赏问题

  • ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计