Aug-14-2018, 12:44 PM
Dear Python Experts,
I am trying to interate through folders in my root directory.
With the start of the first interation (folder) a new folder should be created
named existingname+'_flip'.
I then want to look through the files of the original folder and copy them over to the new folder.
The copy code is not realized yet, for now I want to get the loop right but I struggle.
While I loop through "dirs" the following error comes:
for file_path in os.listdir(path):
TypeError: listdir: path should be string, bytes, os.PathLike or None, not tuple
It seems, I got the input type to the function wrong. How can I convert "path" to string and is
that really the right way?
I am trying to interate through folders in my root directory.
With the start of the first interation (folder) a new folder should be created
named existingname+'_flip'.
I then want to look through the files of the original folder and copy them over to the new folder.
The copy code is not realized yet, for now I want to get the loop right but I struggle.
While I loop through "dirs" the following error comes:
for file_path in os.listdir(path):
TypeError: listdir: path should be string, bytes, os.PathLike or None, not tuple
It seems, I got the input type to the function wrong. How can I convert "path" to string and is
that really the right way?
def main(): #interate through tub folders rootdir = 'C:/Users/xxx/Downloads/xxx/tubs_J/thinktank/' for dirs in os.walk(rootdir): #create new folder = existing name + _flip outPath = os.path.join(dirs, '_flip') if not os.path.exists(outPath): os.makedirs(outPath) path = dirs #iterate through the names of contents of the folder for file_path in os.listdir(path): print(file_path) #write file then save newpathwouldbe = os.path.join(outPath, file_path) if __name__ == '__main__': main()