I’m trying to pass elements from a list to a for loop and of course am getting the classic error ‘argument 1 must be a string not list’ – for the os.chdir() function.
Here is a my code, any suggestions as to how I can get around the above error and still pass the elements of my list on to the rest of the script so it loops through each one would be greatly appreciated!!
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)
You want
os.chdir(x)instead ofos.chdir(path).pathis the list containing all the paths (and should thus probably be namedpaths), so you can’t use it as an argument tochdir.