r/Python • • Sep 15 '20

Resource Python 3.9: All You need to know šŸ‘Š

https://ayushi7rawat.hashnode.dev/python-39-all-you-need-to-know
1.2k Upvotes

204 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Sep 15 '20

[deleted]

142

u/kankyo Sep 15 '20

Those people would have done s[:-4] previously anyway. Using the new stuff is WAY WAY better.

0

u/[deleted] Sep 15 '20

[deleted]

52

u/Ph0X Sep 15 '20 edited Sep 15 '20

I'm a man of culture, I do s.rsplit('.', 1)[0]

0

u/[deleted] Sep 15 '20

[deleted]

9

u/Ph0X Sep 15 '20

It's ambiguous which of the two behaviors is correct in that case, but if you want to remove all extensions, you can just switch to normal split. Of course that will break if it contains a period in the name, but that's also ambiguous. I guess you need a certain level of knowledge about what you're trying to achieve.

7

u/yad76 Sep 15 '20

I don't think it's ambiguous at all. It is a gzip file and so it has a `.gz` extension and your `rsplit` gets the correct result. The `.tar` is just reflecting the name of the gzipped file and not part of the extension of the current gzip file that we are currently concerned with.

3

u/Brian Sep 16 '20

I would agree for that case, but I would say one place where it does give the wrong answer (or at least, a different answer from splitext etc) is with dotfiles. Ie '.config' denotes a hidden file on unix, and pathlib and splitext will treat it as the stem '.config' with empty extension, rather than an empty sten with ".config" suffix.

→ More replies (1)
→ More replies (2)
→ More replies (3)

5

u/I_Say_Fool_Of_A_Took Sep 15 '20

This is the way. I'd never trust that the extension is going to be 3 chars. Aiff, wave, for instance.

15

u/super-porp-cola Sep 15 '20

I mean you don't even have to get obscure, there's .jpeg, .docx, and of course .py.

4

u/mipadi Sep 15 '20

Actually, the way is os.path.splitext(s)[0]. ;-)

→ More replies (3)

16

u/enjoytheshow Sep 15 '20

I feel personally attacked

1

u/garbagekr Sep 15 '20

I’m one of those but I’m new. Can you give a quick example of what I should be doing using pathlib ?

10

u/kankyo Sep 15 '20

Path('foo.txt').stem

→ More replies (5)
→ More replies (17)

7

u/ianliu88 Sep 15 '20

Nice! Didn't know about pathlib.

3

u/eldrichride Sep 15 '20

VFX folk here are stuck in 2.7 and can only dream of Pathlib.

→ More replies (2)

4

u/c00lnerd314 Sep 15 '20

Out of curiosity, is there a downside to using this?

file_name.split('.')[-1]

13

u/call_me_arosa Sep 15 '20

Files don't necessarily have extensions

→ More replies (1)

24

u/[deleted] Sep 15 '20

[removed] — view removed comment

→ More replies (1)

12

u/DarFtr Sep 15 '20

I don't know for sure, but files can have multiple extension such as file that are in download state that are something.mp4.crowndowload It's just a guess anyway, I think it would usually work

→ More replies (1)

8

u/scruffie Sep 15 '20
path.with.dots/file
../file
.dotfile
...manydots
..

If you're not using pathlib, you should be using os.path.splitext, which handles all the above cases (and works with both bytes and strings).

1

u/copperfield42 python enthusiast Sep 15 '20 edited Sep 15 '20

If you want the extension only, this approach fail if you give it a file that doesn't have it for whatever reason, and if you want the name without the extension, doing file_name.split(".")[0] for example fail to consider that there's absolutely nothing stopping you, your user or anything else from using "." in the file name (or path) for whatever reason, for example "my.test.file.txt" is a perfectly valid file name.

Is better to use a function that already have all those things in consideration like os.path.splitext

1

u/yvrelna Sep 16 '20

It'll break if you have something like file_name = "/etc/nginx/conf.d/foobar".

→ More replies (1)

1

u/copperfield42 python enthusiast Sep 15 '20 edited Sep 15 '20

Since I founded it, I always use os.path.splitext for those things... I didn't know about pathlib