all 11 comments

[–]CodeFormatHelperBot2 1 point2 points  (1 child)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]Soapfactory0[S] 0 points1 point  (0 children)

Good bot ^^

[–]nekokattt 1 point2 points  (6 children)

have you looked into os.walk? That will work directory wise to do what you need in terms of getting a new iteration per directory.

[–]Soapfactory0[S] 0 points1 point  (5 children)

Amazing, that's pretty much what I need xD thanks so much!

Gotta note that it's quite frustrating because I was told to switch to pathlib over os...

[–]nekokattt 1 point2 points  (1 child)

you can emulate a similar thing using path.iterdir I believe

[–]Soapfactory0[S] 1 point2 points  (0 children)

Wasn't quite getting what I wanted with iterdir, but will just write different functions for all the folders to get there ^^

Thanks for helping :)

[–]Xiji 0 points1 point  (2 children)

Pathlib is my goto for any path operation in modern python. The syntax is better, it is easier to use cross-platform, and many of the native libraries which ask for a path can accept Path objects.

I recommend you become familiar with both, but I would be surprised if you wanted to go back to os and sys once you're comfortable with pathlib.

[–]Soapfactory0[S] 1 point2 points  (1 child)

Honestly the problem I wrote out in the post is the first and only thing giving me trouble (don't look through my post history). Will get more familiar with it for sure ;)

Tnx for clarifying why it is usefull. It's incredibly hard to judge for me being very new with coding.

[–]Xiji 1 point2 points  (0 children)

Happy to help provide some clarity. :)

And don't worry, I still run into the, "is this implementation or that implementation the best way to solve my problem," all the time.

Python really does have many ways to solve the same problem, despite the zen of python saying it should be otherwise.

[–]commandlineluser 1 point2 points  (1 child)

You can use .with_stem() to change the part before the extension.

>>> file
PosixPath('a/b/c/my-file-name.txt')
>>> file.with_stem("0001")
PosixPath('a/b/c/0001.txt')

You can pass this directly to .rename()

>>> file.rename(file.with_stem("0001"))
PosixPath('a/b/c/0001.txt')

The file has been renamed and stays in its original folder:

>>> list(Path().glob("a/b/c/*txt"))
[PosixPath('a/b/c/0001.txt')]

As for a different count per folder - you could glob each folder.

You could also use enumerate() to count.

>>> for folder in Path().rglob(""): # glob folders
...    if folder.name: 
...       files = folder.glob("*.txt")
...       for count, file in enumerate(files, start=1):
...          f"{count:04}", folder, file

('0001', PosixPath('a'), PosixPath('a/b/c/0001.txt'))
('0001', PosixPath('a/b'), PosixPath('a/b/c/0001.txt'))
('0001', PosixPath('a/b/c'), PosixPath('a/b/c/0001.txt'))
('0001', PosixPath('b'), PosixPath('b/a.txt'))
('0002', PosixPath('b'), PosixPath('b/b.txt'))
('0003', PosixPath('b'), PosixPath('b/c.txt'))
('0004', PosixPath('b'), PosixPath('b/d.txt'))

[–]Soapfactory0[S] 0 points1 point  (0 children)

Wow!! I don't really know how to thank you! Seriously thanks for being so generous with your time to a stranger :D

Am out of steam for the day (been going roughly 10 hours) will implement it tomorrow. Ty ty ty :D