How to return which element from the tuple matched in the endswith function

Ayush Mandowara

I am trying to make a file renamer using Python. I was able to successfully scrape Wikipedia for list of episodes, but while making the renamer file I was met with a lot of discrepancies. What I want is that instead of '.mkv' at the end I want to use exactly the extension that was matched from the if condition. Is there a way to return it?

extensions = ('.webm','.mkv','.flv','.vob','.ogv', 
  '.ogg','.drc','.gif','.gifv','.mng','.avi','.mov', 
  '.qt','.wmv','.yuv','.rm','.rmvb','.asf','.amv','.mp4',
  '.m4p', '.m4v','.mpg', '.mp2', '.mpeg', '.mpe', '.mpv',
  '.mpg', '.mpeg', '.m2v','.m4v','.svi','.3gp','.3g2','.mxf',
  '.roq','.nsv','.f4v', '.f4p', '.f4a' ,'.f4b','.srt')
list = f.readlines()


y = 0
num = 1
for filename in os.listdir(path):
    if filename.endswith(extensions):
      os.rename(path+"\\"+filename,path+"\\"+str(num)+' - '+list[int(y)].strip('\n')+'.mkv') #instead of mkv, I want extension which was matched in the above if condition. 
    y += 1
    num += 1
hspandher

Well either you have to loop over the extensions one by one, or you could split the filename to get the extension.

Split by filename

for filename in os.listdir(path):
    if filename.endsswith(extensions):
        extension = filename.split('.')[-1] # you can use os.path.splitext too as Max Chretien suggested
        # ...

Use explicit loop

for filename in os.listdir(path):
    matching_extensions = filter(lambda extension: filename.endswith(extension), extensions)
    if matching_extensions:
        extension = matching_extensions[0]
    # ...

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to return pattern-matched element?

分類Dev

How to return a Future<User> from function which returns User?

分類Dev

How can I return the last element number from a tuple that satisfies a condition?

分類Dev

Get element from which onclick function is called

分類Dev

F# - Getting fst element of tuple from option type function

分類Dev

How to return a list from a function?

分類Dev

Swift: Get an element from a tuple

分類Dev

Execution of endswith function

分類Dev

Access function return type as Tuple in C#

分類Dev

Element by element comparison in tuple to return set of sets - python

分類Dev

How to return value from debounced function in javascript?

分類Dev

How to get the return values from a function?

分類Dev

How to correctly return value from function?

分類Dev

React native how to return a string from a function

分類Dev

how to return observable from function that has a promise

分類Dev

How to return value from a child process in a function?

分類Dev

How to get a return value from a connect function

分類Dev

How to return result in series from R function?

分類Dev

How to return value from an extended casperjs function?

分類Dev

How to get matched sub documents from mongodb?

分類Dev

How do I return 404 response if no URL Rewrite Rules are matched

分類Dev

How do I zip a single element and tuple into one tuple?

分類Dev

Pandas AssertionError when applying function which returns tuple containing list

分類Dev

How to know from which perl module or file the function was called

分類Dev

Can't edit a tuple from inside a function

分類Dev

use a function which return table, hibernate, postgresql

分類Dev

Calling an unmanaged function which return object by value

分類Dev

Initialize variable as function which return value

分類Dev

How to get which element was clicked?

Related 関連記事

  1. 1

    How to return pattern-matched element?

  2. 2

    How to return a Future<User> from function which returns User?

  3. 3

    How can I return the last element number from a tuple that satisfies a condition?

  4. 4

    Get element from which onclick function is called

  5. 5

    F# - Getting fst element of tuple from option type function

  6. 6

    How to return a list from a function?

  7. 7

    Swift: Get an element from a tuple

  8. 8

    Execution of endswith function

  9. 9

    Access function return type as Tuple in C#

  10. 10

    Element by element comparison in tuple to return set of sets - python

  11. 11

    How to return value from debounced function in javascript?

  12. 12

    How to get the return values from a function?

  13. 13

    How to correctly return value from function?

  14. 14

    React native how to return a string from a function

  15. 15

    how to return observable from function that has a promise

  16. 16

    How to return value from a child process in a function?

  17. 17

    How to get a return value from a connect function

  18. 18

    How to return result in series from R function?

  19. 19

    How to return value from an extended casperjs function?

  20. 20

    How to get matched sub documents from mongodb?

  21. 21

    How do I return 404 response if no URL Rewrite Rules are matched

  22. 22

    How do I zip a single element and tuple into one tuple?

  23. 23

    Pandas AssertionError when applying function which returns tuple containing list

  24. 24

    How to know from which perl module or file the function was called

  25. 25

    Can't edit a tuple from inside a function

  26. 26

    use a function which return table, hibernate, postgresql

  27. 27

    Calling an unmanaged function which return object by value

  28. 28

    Initialize variable as function which return value

  29. 29

    How to get which element was clicked?

ホットタグ

アーカイブ