use regex to extract file path and save it in python

  • Last Update :
  • Techknowledgy :

You can simplify your regex to this:


try this using pandas:

df = pd.read_csv('yourfile.txt', header = None)
df.columns = ['paths']
# pandas string method extract takes a regex


       0 1
       0 2007 05
       1 2007 06
       2 2007 07
       3 2007 08

Suggestion : 2

Last Updated : 25 Jul, 2022



We can use a regular expression to match the file name with the specific pattern.

Pattern - [\w] + ? ( ? = \.)

Suggestion : 3

If the string ends with a backslash, as it will for paths that don’t specify a filename, the regex won’t match at all. When it does match, it will match only the filename, so we don’t need to use any capturing ...,Extracting the filename from a string known to hold a valid path is trivial, even if you don’t know whether the path actually ends with a filename.,The filename always occurs at the end of the string. It can’t contain any colons or backslashes, so it cannot be confused with folders, drive letters, or network shares, which all use backslashes and/or colons.,You have a string that holds a (syntactically) valid path to a file or folder on a Windows PC or network, and you want to extract the filename, if any, from the path. For example, you want to extract file.ext from c:\folder\file.ext.

[ ^ \\/:*?"<>|\r\n]+$