将列表元素传递给for循环

Jay*_*Jay 2 python

我正在尝试将元素从列表传递到for循环,当然我得到的经典错误'参数1必须是字符串而不是列表' - 对于os.chdir()函数.

这是我的代码,任何建议,我如何解决上述错误,仍然将我的列表的元素传递给脚本的其余部分,所以它循环通过每一个将非常感谢!!

path= ['C:\\DataDownload\Administrative', 'C:\\DataDownload\Cadastral', 'C:\\DataDownload\Development']
for x in path[:]:
   os.chdir(path)
   #Remove all previous files from the current folder
   for file in os.listdir(path):
     basename=os.path.basename(file)
     if basename.endswith('.DXF'):
        os.remove(file)
     if basename.endswith('.dbf'):
        os.remove(file)
     if basename.endswith('.kmz'):
        os.remove(file)
     if basename.endswith('.prj'):
        os.remove(file)
     if basename.endswith('.sbn'):
        os.remove(file)
     if basename.endswith('.sbx'):
        os.remove(file)
     if basename.endswith('.shp'):
        os.remove(file)
     if basename.endswith('.shx'):
        os.remove(file)  
     if basename.endswith('.zip'):
        os.remove(file)
     if basename.endswith('.xml'):
        os.remove(file)
Run Code Online (Sandbox Code Playgroud)

sep*_*p2k 6

你想要os.chdir(x)而不是os.chdir(path).

path是包含所有路径的列表(因此可能应该命名paths),因此您不能将其用作参数chdir.