Changing name of the file to parent folder name
I have a bunch of folders in my directory. In each of them there is a file, which you can see below:
Regardless the file extension I would like to have the name of this file to be exactly the same as its parent folder, i.e. when considering folder 2023-10-18 I would like to have the file inside 2023-10-18 instead of occultation....
I tried to rename the multiple files by using this thread:
Renaming multiple files in a directory using Python
and here
but unfortunately after application the code like this:
import os
from pathlib import Path
pth = Path(__file__).parent.absolute()
files = os.listdir(pth)
for file in files:
os.rename(os.pth.join(pth, file), os.pth.join(pth, '' + file + '.kml'))
I have an error:
AttributeError: module 'os' has no attribute 'pth'
described here:
AttributeError: 'module' object has no attribute
which says only a little to me, as I am a novice in Python.
How can I auto change the name of all the files in these directories? I need the same filename as the directory name. Is it possible?
UPDATE:
After hint below, my code looks like this now:
import os
from pathlib import Path
pth = Path(__file__).parent.absolute()
files = os.listdir(pth)
for file in files:
os.rename(os.path.join(pth, file), os.path.join(pth, '' + file + '.kml'))
but instead of changing the filename inside the folder list, all the files in the given directory have been changed to .kml. How can I access to the individual files inside the folderlist?
Comments
Post a Comment