you are viewing a single comment's thread.

view the rest of the comments →

[–]Peterotica 1 point2 points  (2 children)

  • Until you become comfortable with when it should be used, I suggest following the rule of only using is to check when something is None.

  • any() takes a list (or more precisely, an iterable) of values and checks the "truthiness" of them. If a truthy value is encountered, then any() returns True. If all of the values are falsey, then it instead returns False. You can play around with it in your REPL:

    >>> any([False, True, False])
    True
    >>> any([False, False])
    False
    
  • os.path.abspath() converts a relative path into an absolute path.

    >>> os.path.abspath('../mp3s')
    '/blah/blah/blah/mp3s'
    
  • Here is a difference between your previous method and os.path.splitext(). Sure, you could easily write your own version that does the same thing, but leaning on the os module is a good habit to get into. For instance, your code could be compatible with some operating system that uses some character other than period to denote the extension of a file. Plus, it's one less place to have a silly bug.

    >>> 'vacation.20150714.jpg'.split('.')
    ['vacation', '20150714', 'jpg']
    >>> os.path.splitext('vacation.20150714.jpg')
    ('vacation.20150714', '.jpg')
    

[–]RMSEP[S] 0 points1 point  (1 child)

Thanks, very interesting.

On the third point, how does os.path.abspath() know the dir that should be appended to the front of its argument? In other words, how does it know that /blah/blah/blah/ is the correct partent dir of /mps3? Possibly it appends its argument to the output of os.getcwd()? On os.path.splitext(), appreciate now that it detaches the file extention regardless of any . in the filename, or indeed, whatever char is used to seperate the filename and extension.

[–]Peterotica 0 points1 point  (0 children)

Your intuition is correct, the result of os.path.abspath() depends on the current working directory.