Reputation: 19526
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.
Upvotes: 0
Views: 3295
Reputation: 25599
Well, if you have distinct location, eg C:\ , D:\ etc, you can just split on these characters
import re
f=open("file")
for line in f:
print re.split("[C-Z]:",line)[0]
f.close(0
Upvotes: 1
Reputation: 60604
Do you have periods (.
) in your file names, other than at the end right before the extension? If not, you should be able to parse something like this:
1 find first instance of '.'
2 step to the next space
3 that space is the delimiter between file name and location
Upvotes: 1
Reputation: 65619
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)
Upvotes: 2