lotus
lotus

Reputation: 111

How to remove a directory path from a text file

I have a text file with these lines.

1.inputlist

D:\Dolby_Harmanious_kit\DRY_run_kits\Dolby_Digital_Plus_Decoder_Imp\Test_Materials\Test_Signals\ITAF_Tests\seamless_switch\acmod21_I0D0CRC.ec3 -#tD:\Dolby_Harmanious_kit\DRY_run_kits\Dolby\m1_m28_switch.cfg

2.inputlist

D:\Dolby_Harmanious_kit\DRY_run_kits\Dolby_Digital_Plus_Decoder_Imp\Test_Materials\Test_Signals\ITAF_Tests\seamless_switch\acmod_2_252_ddp.ec3 -#tD:\Dolby_Harmanious_kit\DRY_run_kits\Digital\m1_m7_switch_every_3frames.cfg

Here i need to remove the path names like

"D:\Dolby_Harmanious_kit\DRY_run_kits\Dolby_Digital_Plus_Decoder_Imp\Test_Materials\Test_Signals\ITAF_Tests\seamless_switch\" and "D:\Dolby_Harmanious_kit\DRY_run_kits\Digital\ "

.Note that all lines have a different path names.I have a example code to remove a path name.

Code:

import re
b = open ('Filter_Lines.txt','w')
with open('Lines.txt') as f:
    for trim in f:
        repl = (re.sub('D:.*\\\\','',trim).rstrip('\n'))
        b.write(repl + '\n')

b.close()

But here this removes a whole text from "

D:\Dolby_Harmanious_kit\DRY_run_kits\Dolby_Digital_Plus_Decoder_Imp\Test_Materials\Test_Signals\ITAF_Tests\seamless_switch\acmod21_I0D0CRC.ec3 -#tD:\Dolby_Harmanious_kit\DRY_run_kits\Dolby\"

.I need to remove only path names not including "acmod21_I0D0CRC.ec3" in that line.

Can you please guide me for this.

Upvotes: 1

Views: 122

Answers (1)

Mohideen bin Mohammed
Mohideen bin Mohammed

Reputation: 20206

I did upto what i understand your question, here you specified path's are not similar i.e what i understood is, your path might be

a) D://a/b/c/file_name.cfg -#tD://a/b/c/d/e/file_name.cfg

is it correct what i understood?

here 2 path present in single line, but common thing is its contains -#t, so simply use split method to split that.

here what i did based i understand from your post,

import re
li = []
b = open ('file_sample.txt','w')
with open ('file_sam.txt') as f:
    for i in open ('file_sam.txt','r'):
        a =  [re.sub('.*\\\\','',i).rstrip('\n') for i in i.split('D:')]
        b.write(''.join(a) + '\n')   
b.close()

here my inputs are,

'D:\Dolby_Harmanious_kit\DRY_run_kits\Dolby_Digital_Plus_Decoder_Imp\Test_Materials\Test_Signals\ITAF_Tests\seamless_switch\acmod21_I0D0CRC.ec3 -#tD:\Dolby_Harmanious_kit\DRY_run_kits\Dolby\m1_m28_switch.cfg'
'D:\Dolby_Harmanious_kit\DRY_run_kits\Dolby_Digital_Plus_Decoder_Imp\Test_Materials\Test_Signals\ITAF_Tests\seamless_switch\acmod_2_252_ddp.ec3 -#tD:\Dolby_Harmanious_kit\DRY_run_kits\Digital\m1_m7_switch_every_3frames.cfg'

it gives me,

'acmod21_I0D0CRC.ec3 -#tm1_m28_switch.cfg'
'acmod_2_252_ddp.ec3 -#tm1_m7_switch_every_3frames.cfg'

is this you want?

Upvotes: 1

Related Questions