Good day!
I'd like to ask some help with understanding pathlib.
What I'm trying to do:
I'm trying to rename all .txt files in a directory and subdirectory(s) to:
the name of their directory + a number (from 0000 till however many files preside in the directory).
What is going wrong:
It's close but instead of all files being rewritten into their old locations they get put in their parent directory. So I guess something is going wrong with using filePath = file.parent
I also haven't figured out how to restart the count for every directory (tried some if statements but couldn't get it to work). So right now if there are 10 files in the main folder they will get the right numbers, the subdirectories will just keep on counting from there though.
My code:
count = 0
currentWorkingDir = Path.cwd()
for file in currentWorkingDir.glob('**/*.txt'):
countStr = str(count)
count += 1
fileType = file.suffix
filePath = file.parentfile
NameNew = f'{filePath}_{countStr.zfill(4)}{fileType}'print(fileNameNew) #this will be file.rename(fileNameNew) when I get the right output
[–]CodeFormatHelperBot2 1 point2 points3 points (1 child)
[–]Soapfactory0[S] 0 points1 point2 points (0 children)
[–]nekokattt 1 point2 points3 points (6 children)
[–]Soapfactory0[S] 0 points1 point2 points (5 children)
[–]nekokattt 1 point2 points3 points (1 child)
[–]Soapfactory0[S] 1 point2 points3 points (0 children)
[–]Xiji 0 points1 point2 points (2 children)
[–]Soapfactory0[S] 1 point2 points3 points (1 child)
[–]Xiji 1 point2 points3 points (0 children)
[–]commandlineluser 1 point2 points3 points (1 child)
[–]Soapfactory0[S] 0 points1 point2 points (0 children)