python regular expression to extract file_name from a given directory path

Ayushman Buragohain

I have some folders like-so:

../dog_breeds/images/Images/n02085620-Chihuahua
../dog_breeds/images/Images/n02085782-Japanese_spaniel
../dog_breeds/images/Images/n02086910-papillon
../dog_breeds/images/Images/n02088466-bloodhound
....
....

I want to extract only these info's(Chihuahua, Japanese_spaniel, papillon, bloodhound) from the paths of the files using Python. Can anybody help me ?

Rakesh

You can use str.split here

Ex:

s = """../dog_breeds/images/Images/n02085620-Chihuahua
../dog_breeds/images/Images/n02085782-Japanese_spaniel
../dog_breeds/images/Images/n02086910-papillon
../dog_breeds/images/Images/n02088466-bloodhound
"""

for p in s.splitlines():
    print(p.split("-")[-1])

If you need regex.

import re

for p in s.splitlines():
    print(re.search(r"\-(\w+)$", p).group(1))

Output:

Chihuahua
Japanese_spaniel
papillon
bloodhound

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

Regular expression to extract file name?

分類Dev

Regular expression doesn't extract whole the id from a log file?

分類Dev

Extract DNA from FASTA with regular expression

分類Dev

Regular expression java to extract the balance from a string

分類Dev

How to extract all the Id's value from the Json given below using Regular expression extractor or any other Extractor(Xpath, Beanshell etc)

分類Dev

In Python, how should one extract the second-last directory name in a path?

分類Dev

Python Substitute nth Match from a Regular Expression

分類Dev

Select all files from directory that contain file with given name

分類Dev

How to extract expression path and value from Spring query dsl Predicate?

分類Dev

Extract data using regular expression

分類Dev

Regular expression in boost to extract information

分類Dev

regular expression to extract numerical values

分類Dev

regex expression for extracting a base file name from a path

分類Dev

Get directory name from full directory path regardless of trailing slash

分類Dev

Regular Expression that does not match a given sequence

分類Dev

Regular expression to extract full content inside a div

分類Dev

Understanding a regular expression in a series extract function in pandas

分類Dev

regular expression to extract a string with ruby regex

分類Dev

Extract segment from a file between two given positions in bash

分類Dev

Nginx location directive with regular expression to extract folder name and using in try_files

分類Dev

CMD - check folder path for a regular expression

分類Dev

How to extract variables from log file path, test log file name for pattern in Logstash?

分類Dev

Extract server name from UNC path using Excel

分類Dev

Regular expression pattern to replace in Python

分類Dev

Creating a dictionary from a given text file (python)

分類Dev

Path for file in Directory

分類Dev

Path for file in Directory

分類Dev

Regular expression to parse log file

分類Dev

Python: import file from sibling directory, where parent directory has the same name

Related 関連記事

  1. 1

    Regular expression to extract file name?

  2. 2

    Regular expression doesn't extract whole the id from a log file?

  3. 3

    Extract DNA from FASTA with regular expression

  4. 4

    Regular expression java to extract the balance from a string

  5. 5

    How to extract all the Id's value from the Json given below using Regular expression extractor or any other Extractor(Xpath, Beanshell etc)

  6. 6

    In Python, how should one extract the second-last directory name in a path?

  7. 7

    Python Substitute nth Match from a Regular Expression

  8. 8

    Select all files from directory that contain file with given name

  9. 9

    How to extract expression path and value from Spring query dsl Predicate?

  10. 10

    Extract data using regular expression

  11. 11

    Regular expression in boost to extract information

  12. 12

    regular expression to extract numerical values

  13. 13

    regex expression for extracting a base file name from a path

  14. 14

    Get directory name from full directory path regardless of trailing slash

  15. 15

    Regular Expression that does not match a given sequence

  16. 16

    Regular expression to extract full content inside a div

  17. 17

    Understanding a regular expression in a series extract function in pandas

  18. 18

    regular expression to extract a string with ruby regex

  19. 19

    Extract segment from a file between two given positions in bash

  20. 20

    Nginx location directive with regular expression to extract folder name and using in try_files

  21. 21

    CMD - check folder path for a regular expression

  22. 22

    How to extract variables from log file path, test log file name for pattern in Logstash?

  23. 23

    Extract server name from UNC path using Excel

  24. 24

    Regular expression pattern to replace in Python

  25. 25

    Creating a dictionary from a given text file (python)

  26. 26

    Path for file in Directory

  27. 27

    Path for file in Directory

  28. 28

    Regular expression to parse log file

  29. 29

    Python: import file from sibling directory, where parent directory has the same name

ホットタグ

アーカイブ