[好吧,我已经花了一段时间了,看着教程和论坛来尝试找出问题所在,但是我一直收到同样的“ IndexError:列表索引超出范围”错误。我正在尝试制作一个程序,将特定文件夹中的所有文件排序到另一个有组织的文件夹中。 (例如,使用“下载”文件夹,并在其中创建一堆文件夹,这些文件可以整洁地生活在其中)
这是我的代码:
import os import shutil path = "D:/Python Programs/Automatic File Sorter" names = os.listdir(path) folder_name = ["Images", "Videos", "Documents", "exe", "misc"] a = 0 b = 0 c = 0 d = 0 img = [".jpg", ".jpeg", ".png", ".rw2", ".xcf", ".xmp"] video = [".WEBM" ".MPG", ".MP2", ".MPEG", ".MTS", ".MPE", ".flv", ".MPV", ".OGG", ".MP4", ".M4P", ".M4V", ".AVI", ".WMV", ".MOV", ".QT", ".FLV", ".SWF", ".AVCHD"] doc = [".txt", ".docx", ".pdf", ".pds", ".xlsx", ".pptm", ".obj", ".mtl", ".stl"] exe = [".exe", ".msi"] for x in range (0, 5): if not os.path.exists(path + "/" + folder_name[x]): os.makedirs(path + "/" + folder_name[x]) x = 0 i = 0 for x in names[i]: print("a: " + str(a)) print("b: " + str(b)) print("c: " + str(c)) print("d: " + str(d)) print("i: " + str(i)) if i <= len(names): if a <= 5: if img[a] in names[i] and not os.path.exists(path + "/" + "Images" + names[i]): shutil.move(path + "/", path + "/" + "Images/" + names[i]) a += 1 if b <= 18: if video[b] in names[i] and not os.path.exists(path + "/" + "Videos" + names[i]): shutil.move(path + "/", path + "/" + "Videos/" + names[i]) b += 1 if c <= 8: if doc[c] in names[i] and not os.path.exists(path + "/" + "Documents" + names[i]): shutil.move(path + "/", path + "/" + "Documents/" + names[i]) c += 1 if d <= 1: if exe[d] in names[i] and not os.path.exists(path + "/" + "exe" + names[i]): shutil.move(path + "/", path + "/" + "exe/" + names[i]) d += 1 i += 1
[运行时,只有
i
的值增加,而其他变量没有。其他if语句由于names[i]
由于某种原因超出范围而无法运行。任何帮助表示赞赏!
[好吧,我已经花了一段时间了,看着教程和论坛来尝试找出问题所在,但是我一直收到同样的“ IndexError:列表索引超出范围”错误。我正在尝试...
因为名称是目录列表。所以