Question

I have a text file which is not suitable for splitting and I want to print out the path and filename from each file.

Lines can look like this:

"sometextC:\folder\folder\filename.exesometext"
"sometext C:\folder\filename.exe sometext"

and so on...

I want something like this:

for line in inputFile.readlines():
    hit = find everything between C:\ and .exe
    print hit

output should look like this:

C:\folder\folder\file.exe 
C:\folder\file.exe

WHERE folder and file is changed with whatever is found in the text. Sorry if my first post was a bit unclear. Number of parent dirs etc can vary a lot.

Thanks!

Était-ce utile?

La solution

As a first approximation, you could use re.finditer to find things that might be paths. The example shown below might also match things which are not valid paths (and I'm not strong on the rules for paths on Windows, so you should test that this does what you want thoroughly):

contents = inputFile.read()
for path in re.finditer("(C:\\.*?\.exe)", contents):
    print path

Autres conseils

You can use

for path in re.finditer("(C:\Users\Default\thanks.exe)", contents):
print path

to print the path

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top