#πŸ”’ Name and exceptions of a file

94 messages Β· Page 1 of 1 (latest)

hearty shoal
#

Hey! Im new to python and i got a problem, if someone can help me it will be great. So i have a folder on my pc and in that folder i have lots of different files, i want to get the names and the exception of it. but i want to make them separated. how can i do it?

wanton remnantBOT
#

@hearty shoal

Python help channel opened

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.

hearty shoal
#

I got this but its not what i wanted

meager berry
#

what do you want?

#

can you show an example

hearty shoal
#

example:
Name: "GTA5"
Exception: ".exe"

#

something like that

#

@meager berry

solar flame
#

That's called the extension, not the exception, btw

ruby wigeon
#

!os.splitext

#

uh sorry

#

sigh

meager berry
#

you can assign each part to a variable and print them separately

ruby wigeon
#

!d os.path.splitext

hearty shoal
#

thx

wanton remnantBOT
#

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...
ruby wigeon
#

^ calling this function will help you get the file name separated from the ext

#

πŸ€¦β€β™‚οΈ which i see you're already doing

solar flame
#

What do you mean by "make them separated"?

hearty shoal
meager berry
#

name, extension = os.path.splittext()

ruby wigeon
hearty shoal
#

oh alr

#

so wait what do I need to do here

#

to make it work

solar flame
#

What exactly are you trying to do? What do you mean by "make them separate"?

meager berry
hearty shoal
#

so i got this

#

but i want to make it like that:
Name: "GTA5"
Extension: ".exe"

ruby wigeon
solar flame
#

And then you can do separate prints with the name and extension variables

hearty shoal
ruby wigeon
hearty shoal
#

umm

#

letme think

#

to the print?

#

guys im sorry if i look stupid, im kinda new in python

solar flame
#

Can you explain what a for-loop does? Okay if you can't

hearty shoal
#

not really

solar flame
#

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.

hearty shoal
#

ohhh

solar flame
#

So like, the first time it'll be "GTA5.exe", then it'll be "GTA6.exe", then it'll be "Screenshot1.png", etc.

hearty shoal
#

yeah i understand now

solar flame
#

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

hearty shoal
#

mhm

#

now i understand

solar flame
#

Awesome!

hearty shoal
#

so the splittext does separate the name and the extension of the file right?

solar flame
#

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.

solar flame
hearty shoal
#

so i need to put that in print()?

#

like that print(name, extension = os.path.splittext(file_name))?

solar flame
#

no, print just writes text to the console.

hearty shoal
#

right

#

so i dont understand how can i use this: name, extension = os.path.splittext(file_name)

solar flame
#

Can you explain what a variable is?

hearty shoal
#

yeah

#

example:
x = car

#

i dont really know how to explain that

solar flame
#

What do you expect this code to do?

x = "car"
print(x)
hearty shoal
#

car

solar flame
#

what do you expect this code to do?

x,y = "cat", "dog"
print(y)
print(x)
hearty shoal
#

cat, dog

solar flame
#

Not quite

#

Each print statement will print on a separate line

hearty shoal
#

oh

solar flame
#

And it prints y before x

hearty shoal
#

ohh right

#

so it will be :
dog
cat

solar flame
#

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').

hearty shoal
#

yeah

solar flame
#

And what we want to do is assign it to 2 separate variables

hearty shoal
#

right

solar flame
#

Do you know what to do once you have the 2 separate variables?

hearty shoal
#

not really

solar flame
#

Are you familiar with fstrings, which look like f"your number is {5}"?

hearty shoal
#

i did learn them but i cant really understand how they work

solar flame
#

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"
hearty shoal
#

ohhh

#

i undersstand now

solar flame
#

Cool, is that enough for you to solve your problem?

hearty shoal
#

yeah, thanks a lot

solar flame
#

My pleasure!

wanton remnantBOT
#
Python help channel closed

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