문제

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!

도움이 되었습니까?

해결책

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

다른 팁

You can use

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

to print the path

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top