#π Name and exceptions of a file
94 messages Β· Page 1 of 1 (latest)
@hearty shoal
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
That's called the extension, not the exception, btw
you can assign each part to a variable and print them separately
oh
!d os.path.splitext
thx
os.path.splitext(path)```
Split the pathname *path* into a pair `(root, ext)` such that `root + ext == path`, and the extension, *ext*, is empty or begins with a period and contains at most one period.
If the path contains no extension, *ext* will be `''`:
```py
>>> splitext('bar')
('bar', '')
``` If the path contains an extension, then *ext* will be set to this extension, including the leading period. Note that previous periods will be ignored...
^ calling this function will help you get the file name separated from the ext
π€¦ββοΈ which i see you're already doing
What do you mean by "make them separated"?
im already did that i think
name, extension = os.path.splittext()
Yeah, i didn't actually look at your code first. My bad
What exactly are you trying to do? What do you mean by "make them separate"?
.
do this
right, so you can use tuple unpacking to store them in separate variables, like this.
And then you can do separate prints with the name and extension variables
where do i need to put that
What does your intuition tell you?
umm
letme think
to the print?
guys im sorry if i look stupid, im kinda new in python
Can you explain what a for-loop does? Okay if you can't
not really
Okay. The for-loop will execute all the code inside of it (in the case of the program you currently have, print(os.path.splittext(file_name))) over and over again, but each time it will change what file_name refers to, going thru the list_of_files.
ohhh
So like, the first time it'll be "GTA5.exe", then it'll be "GTA6.exe", then it'll be "Screenshot1.png", etc.
yeah i understand now
Right now what you're doing is using the splittext function each time, and you're just printing the output of the splittext function. It seems you want to be processing it a bit more.
Using name, extension = os.path.splittext(file_name) will create 2 variables (name and extension) which contain the name and the extension of the filename, respectively.
You can use those variables in separate print statements to get the output you're looking for
Awesome!
so the splittext does separate the name and the extension of the file right?
What splittext really does is it returns a tuple with 2 items, one of which is the name, and the other is the extension. That's what's being printed in the version of the program in the screenshot.
the syntax here unpacks that tuple into 2 separate variables that you can process separately
so i need to put that in print()?
like that print(name, extension = os.path.splittext(file_name))?
no, print just writes text to the console.
right
so i dont understand how can i use this: name, extension = os.path.splittext(file_name)
Can you explain what a variable is?
What do you expect this code to do?
x = "car"
print(x)
car
what do you expect this code to do?
x,y = "cat", "dog"
print(y)
print(x)
cat, dog
oh
And it prints y before x
Exactly
name, extension = os.path.splittext(file_name) is doing the same thing, except it's a bit obscured.
The parts of print(os.path.splittext(file_name)), include
print, which will write whatever is in-between the parentheses to the screen,os.path.splittext(file_name), which will evaluate (the first time) as('GTA5','.exe')
If you use os.path.splittext(file_name) without the print, then you can do something else with ('GTA5','.exe').
yeah
And what we want to do is assign it to 2 separate variables
right
Do you know what to do once you have the 2 separate variables?
not really
Are you familiar with fstrings, which look like f"your number is {5}"?
i did learn them but i cant really understand how they work
Basically, python will try to parse whatever is inside the brackets, rather than just leave it raw like in an unprefixed string or a raw string.
So like,
x = "cat"
print(f"My favorite animal is a {x}")
``` will print "My favorite animal is a cat"
Cool, is that enough for you to solve your problem?
yeah, thanks a lot
My pleasure!
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.
π Name and exceptions of a file