【问题标题】:Delete comments with python functionDelete comments with python function
【发布时间】:2022-12-02 13:55:37
【问题描述】:

Anybody can advise what could be wrong with my code?

I am trying to make a method that removes the single line comments from the content.

Also, the method should return the single line comments that start with \'#\'.

import os


def deleteComments(file):
    try:
        my_file = open(file, \'r\')
        data = my_file.read()
        clean = \"\"
        comment= 0
            if i[0] == \"#\":
                comment += 1
            else:
                pass
        with open(\"clean-\", \"w\") as f:
            f.write(clean)
            f.close()
        my_file.close()
    except:
        print(\"An error occurred with accessing the files\")
        return file


def deleteComment(file):
    try:
        my_file = open(file, \'r\')
        data = my_file.read()
        clean = \"\"
        comment= 0
            if i[0] == \"#\":
                comment += 1
            else:
                pass
        with open(\"clean-\", \"w\") as f:
            f.write(clean)
            f.close()
        my_file.close()
    except:
        print(\"An error occurred with accessing the files\")
        return file

    标签: python


    【解决方案1】:
    猜你喜欢
    • 2022-12-02
    • 2022-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 2023-01-28
    • 2020-11-29
    相关资源
    最近更新 更多