Aug-04-2020, 09:44 PM
(Aug-04-2020, 08:02 PM)bowlofred Wrote: In your rename, you're asking for only the final component of the pathpath.split(dirpath)[-1]
If you want all the components, then you'll need to grab them without the [-1] restriction. Probably just join all the components with "_" to turn it into a string.
Thank you for responding to my question. I am still struggling with this. I took out the [-1] restriction:
import os from os import walk, path, rename import mimetypes for dirpath, _, files in walk('dir/test2'): for f in files: if mimetypes.guess_type(f)[0] == 'text/plain': rename(path.join(dirpath, f), path.join(dirpath, path.split(dirpath) + '_' + f))And I got the following error:
Output: File "filename2.py", line 8, in <module>
rename(path.join(dirpath, f), path.join(dirpath, path.split(dirpath) + '_' + f))
TypeError: can only concatenate tuple (not "str") to tuple