Python Forum
Append root directory folder and subdirectory to filename
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Append root directory folder and subdirectory to filename
#1
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))
Reply
#2
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.
Reply
#3
(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
Reply
#4
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))
Reply
#5
(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?
Reply
#6
In the place of path.split(dirpath)[-1]
Reply
#7
(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))
Reply
#8
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.
Reply
#9
(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.
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 466 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  delete all files and subdirectory from a main folder mg24 7 1,530 Oct-28-2022, 07:55 AM
Last Post: ibreeden
  Append files and add column with last part of each filename NiKirk 0 2,564 Feb-04-2022, 07:35 AM
Last Post: NiKirk
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,390 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 2,438 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  How to skip a folder directory in a loop mfkzolo 2 12,364 Nov-18-2020, 07:56 AM
Last Post: mfkzolo
  Python Cut/Copy paste file from folder to another folder rdDrp 4 4,945 Aug-19-2020, 12:40 PM
Last Post: rdDrp
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 2,525 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  How to change directory to any folder where python is not installed ? firashelou 4 2,572 Apr-03-2020, 02:43 PM
Last Post: firashelou
  Python 2.7 Import error. Directory and filename conflict petcoo00 2 2,335 Feb-02-2020, 08:46 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020