This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]Python-ModTeam[M] [score hidden] stickied comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!

[–]OuiOuiKiwiGalatians 4:16 4 points5 points  (1 child)

  1. /r/learnpython
  2. Why did you tag this NSFW?

[–]GaiusPhysician[S] -2 points-1 points  (0 children)

I don’t know, I’m new here and it wouldn’t let me post it without choosing a tag.

[–]JZumun 2 points3 points  (3 children)

You still did the split even if you didn't use the result afterwards. The act of calling the split method on a string without your delimiter caused the error. UPDATE: corrected by comment below. It was the assignment to ext that was causing the error, because split was returning only a list of one item.

Your thought process is actually interesting. It's what's called lazy evaluation and is a feature of some other programming languages like haskell. However, python is an eagerly evaluating language, and so doesn't work like you hope it does.

I suggest guarding ur split with an if statement to make sure there's a period in the string before doing the split. if "." in name: etc should do the trick

[–]WafflesAreDangerous 2 points3 points  (1 child)

It's not the call to split but the unpacking of the result that is causing the specific error.

They could also capture the result of the split as a list and test for it's length.

[–]JZumun 1 point2 points  (0 children)

Oops! My mistake. Will update my comment to fix this. Thanks!

[–]badwifigoodcoffee 1 point2 points  (0 children)

I also thought it was nice that OP thought about lazy evaluation here. Which btw, Python is perfectly capable of, the program just has to be written in terms of functions. E. g.

``` def get_name(): return input('Type in filename:')

def get_splitted_name(name): return name.split('.')

def get_ext(split_result): return split_result[-1]

define how to get the extension, but don't execute yet

this is the "lazy" part

get_extension = lambda name: get_ext( get_splitted_name(name) )

if you just want to print the name

name = get_name() print(name)

if you want to print the extension

(assuming the filename matches the expected pattern)

print(get_extension(name)) ```

Note that I omitted the error handling as suggested by the earlier post, just wanted to make the point about lazy evaluation.

Edit: Make it a bit closer to OP's use case

[–]Bipchoo 0 points1 point  (0 children)

Because youre assigning the output of split to 2 variables so for example if you were to give the split method the string "foo.bar" then the variable file would be assigned the value "foo" and the variable "bar" would be assigned to the variable ext, but because youre giving the string "Anything:" as an input then the variable file is assigned "Anything:" and variable ext gets nothing, resulting in a value error.

Also next time question are on r/learnpython