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

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.