Python Forum

Full Version: Append root directory folder and subdirectory to filename
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a few text files, such as a.txt and b.txt, in a location called dir/test2. I would like to rename those two files to dir_test2_a.txt and dir_test2_b.txt.

The code I have so far only renames them to test2_a.txt and test2_b.txt. I'm not sure how to get the preceding folder(s) into the filename as well.

Any help would be appreciated!

import os
from os import walk, path, rename
import mimetypes

for dirpath, _, files in walk('dir/test/test2'):
    for f in files:
    	if mimetypes.guess_type(f)[0] == 'text/plain':
        	rename(path.join(dirpath, f), path.join(dirpath, path.split(dirpath)[-1] + '_' + f))
In your rename, you're asking for only the final component of the path path.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.
(Aug-04-2020, 08:02 PM)bowlofred Wrote: [ -> ]In your rename, you're asking for only the final component of the path path.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
By doing path.split(dirpath)[-1], you're asking for "the last component of the path", which is a str.

If you just remove the bracket, you're asking for all components, which will be a collection (a tuple in this case). You need it to be a string, probably with underscores between them, so you want (another) join. Something like:

"_".join(path.split(dirpath))
(Aug-04-2020, 10:00 PM)bowlofred Wrote: [ -> ]By doing path.split(dirpath)[-1], you're asking for "the last component of the path", which is a str.

If you just remove the bracket, you're asking for all components, which will be a collection (a tuple in this case). You need it to be a string, probably with underscores between them, so you want (another) join. Something like:

"_".join(path.split(dirpath))

I apologize for all these questions, but where do I add that additional join?
In the place of path.split(dirpath)[-1]
(Aug-05-2020, 04:21 PM)bowlofred Wrote: [ -> ]In the place of path.split(dirpath)[-1]

When I run the following code, the text files in my folder just disappears.

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, "_".join(path.split(dirpath)) + '_' + f))
I suggest not renaming the files and instead printing out the path/filename you would use if you did rename. This will quickly point out where you are making a mistake.
(Aug-05-2020, 05:52 PM)deanhystad Wrote: [ -> ]I suggest not renaming the files and instead printing out the path/filename you would use if you did rename. This will quickly point out where you are making a mistake.

I backed up the file in a cloud, so I'm not too worried. I'm trying to rename the files themselves so another program I am using can output the filenames so I can identify the metadata.
My suggestion to use print was to help figure out your name generation problem. Once you are making valid path/filenames the rename stuff should work.