Recursive File Read, update - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Recursive File Read, update

spicysudhispicysudhi Posts: 2Member
hi,

I am very new to python and thanks for your support.

I am trying to write a program to read all the .DAT files in the given folder and its sub folders.

Then in each of the .DAT file, I want to replace a perticular string - this string appears within first 10 lines in the file.

For example: The files have below string on line 4 or 7.
[color=Blue]Name : TESTNAME[/color]

I want to update (append) the name with the filename. The output is like
[color=Blue]Name : FILENAME_TESTNAME[/color]
(FILENAME is the name of the file without .DAT extension)

any help... thanks.

Comments

  • utpalenduutpalendu Posts: 9Member
    #!/usr/bin/env python

    import os
    def get_immediate_subdirectories(dir):
    return [name for name in os.listdir(dir)
    if os.path.isdir(os.path.join(dir, name))]

    def get_file(dir):
    return [name for name in os.listdir(dir)
    if os.path.isfile(os.path.join(dir,name))]

    if __name__=='__main__':
    list=[]
    list=get_immediate_subdirectories(os.getcwd())
    print list
    filelist=[]
    for dir in list:
    fileall=get_file(dir)
    for file in fileall:
    filename,extension=file.split('.')
    if extension=='py':
    filename+='__TESTNAME'
    #filelist.append('in dir='+dir)
    filelist.append(filename)
    print filelist
Sign In or Register to comment.