0

I am working on a code which removes punctuation and numbers from a text file. As i have hundreds of files to process ! Can someone please share a python code which can read filename from a directory in Google Colab and process each file according to the following code. thanks

filename = input("Enter filename: ")
 
 
def remove_punc(string):
    punc = '''!()-[]{};:'"\, <>./?@#$%^&*_~1234567890'''
    for ele in string:  
        if ele in punc:  
            string = string.replace(ele, " ") 
    return string
 
 
try:
    with open(filename,'r',encoding="utf-8") as f:
        data = f.read()
    with open(filename,"w+",encoding="utf-8") as f:
        f.write(remove_punc(data))
    print("Removed punctuations from the file", filename)
except FileNotFoundError:
    print("File not found")

I have tried the above code, it works just fine. Needed to add new feature requested earlier.

  • Use `glob.glob()` to get a list of all the files that match a filename pattern. Loop over the filenames executing your code on each filename. – Barmar Apr 25 '23 at 17:16

0 Answers0