Posts: 14
Threads: 7
Joined: Aug 2019
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))
Posts: 1,583
Threads: 3
Joined: Mar 2020
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.
Posts: 14
Threads: 7
Joined: Aug 2019
(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
Posts: 1,583
Threads: 3
Joined: Mar 2020
Aug-04-2020, 10:00 PM
(This post was last modified: Aug-04-2020, 10:01 PM by bowlofred.)
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))
Posts: 14
Threads: 7
Joined: Aug 2019
(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?
Posts: 1,583
Threads: 3
Joined: Mar 2020
In the place of path.split(dirpath)[-1]
Posts: 14
Threads: 7
Joined: Aug 2019
(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))
Posts: 6,798
Threads: 20
Joined: Feb 2020
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.
Posts: 14
Threads: 7
Joined: Aug 2019
(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.
Posts: 6,798
Threads: 20
Joined: Feb 2020
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.
|