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