Search code examples
pythonwindowsfilenamesstring-parsing

Python String parsing: filenames with spaces


This probably isn't the most common filename parsing problem, but I have a program that displays a list of files in the following format:

Filename.ext Location

Some examples would be

sampleFile.jpg C:\Images\my jpgs
another file.bmp C:\Images\myBmps

The filename and the location is separated by a single space. As shown, I can have spaces in my filename.

I want to extract the filename from each line but can't seem to find a good way to do so. I thought of searching the index of a particular character then extract substring from 0 to (index - offset), where offset is the number of characters I should go back. But I don't think there is a character that I could search on that will guarantee a hardcoded offset would work.


Solution

  • I'd probably uses a regex for grabbing anything that started with a drive letter to the end of the line, something like:

     import re
     matchWinPaths = re.compile("^.*([A-Z]:\\.+$)")
    

    then match each line with

     matches = re.match(line, matchWinPaths)
     winPath = matches.group(1)