Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何删除注释行并在同一文件上写入?_Python - Fatal编程技术网

Python:如何删除注释行并在同一文件上写入?

Python:如何删除注释行并在同一文件上写入?,python,Python,我有一个注释文件列表,需要删除该文件(#)中的注释行,并在同一个文件上写入 注释文件: #Hi this the comment define host { use template host_name google_linux address 192.168.0.12 } #commented config #ddefine host { #d use tem

我有一个注释文件列表,需要删除该文件(#)中的注释行,并在同一个文件上写入

注释文件:

#Hi this the comment
define host {
        use             template
        host_name       google_linux
        address         192.168.0.12
}

#commented config
#ddefine host {
#d      use             template1
#d      host_name       fb_linux
#d      address         192.168.0.13
#d}
>>> with open('commentfile.txt','r+') as file:
...     for line in file:
...        if not line.strip().startswith('#'):
...             print line,
...             file.write(line)
...
define host {
        use             template
        host_name       google_linux
        address         192.168.0.12
}

>>> with open('commentfile.txt','r+') as file:
...     for line in file:
...        if line.strip().startswith('#'):
...             continue
...        print line,
...        file.write(line)
...
define host {
        use             template
        host_name       google_linux
        address         192.168.0.12
}
cat commentfile.txt
#Hi this the comment
    define host {
            use             template
            host_name       google_linux
            address         192.168.0.12
    }

    #commented config
    #ddefine host {
    #d      use             template1
    #d      host_name       fb_linux
    #d      address         192.168.0.13
    #d}
 cat commentfile.txt
 define host {
                use             template
                host_name       google_linux
                address         192.168.0.12
        }
for line in file:
           m = re.match(r'^([^#]*)#(.*)$', line)
           if m:
              continue
我为删除文件中的注释行而编写的代码

代码:

#Hi this the comment
define host {
        use             template
        host_name       google_linux
        address         192.168.0.12
}

#commented config
#ddefine host {
#d      use             template1
#d      host_name       fb_linux
#d      address         192.168.0.13
#d}
>>> with open('commentfile.txt','r+') as file:
...     for line in file:
...        if not line.strip().startswith('#'):
...             print line,
...             file.write(line)
...
define host {
        use             template
        host_name       google_linux
        address         192.168.0.12
}

>>> with open('commentfile.txt','r+') as file:
...     for line in file:
...        if line.strip().startswith('#'):
...             continue
...        print line,
...        file.write(line)
...
define host {
        use             template
        host_name       google_linux
        address         192.168.0.12
}
cat commentfile.txt
#Hi this the comment
    define host {
            use             template
            host_name       google_linux
            address         192.168.0.12
    }

    #commented config
    #ddefine host {
    #d      use             template1
    #d      host_name       fb_linux
    #d      address         192.168.0.13
    #d}
 cat commentfile.txt
 define host {
                use             template
                host_name       google_linux
                address         192.168.0.12
        }
for line in file:
           m = re.match(r'^([^#]*)#(.*)$', line)
           if m:
              continue
我尝试使用上述两种方法打印输出返回正确,但无法再次在同一文件上写入

文件中的输出:

#Hi this the comment
define host {
        use             template
        host_name       google_linux
        address         192.168.0.12
}

#commented config
#ddefine host {
#d      use             template1
#d      host_name       fb_linux
#d      address         192.168.0.13
#d}
>>> with open('commentfile.txt','r+') as file:
...     for line in file:
...        if not line.strip().startswith('#'):
...             print line,
...             file.write(line)
...
define host {
        use             template
        host_name       google_linux
        address         192.168.0.12
}

>>> with open('commentfile.txt','r+') as file:
...     for line in file:
...        if line.strip().startswith('#'):
...             continue
...        print line,
...        file.write(line)
...
define host {
        use             template
        host_name       google_linux
        address         192.168.0.12
}
cat commentfile.txt
#Hi this the comment
    define host {
            use             template
            host_name       google_linux
            address         192.168.0.12
    }

    #commented config
    #ddefine host {
    #d      use             template1
    #d      host_name       fb_linux
    #d      address         192.168.0.13
    #d}
 cat commentfile.txt
 define host {
                use             template
                host_name       google_linux
                address         192.168.0.12
        }
for line in file:
           m = re.match(r'^([^#]*)#(.*)$', line)
           if m:
              continue
预期输出:

#Hi this the comment
define host {
        use             template
        host_name       google_linux
        address         192.168.0.12
}

#commented config
#ddefine host {
#d      use             template1
#d      host_name       fb_linux
#d      address         192.168.0.13
#d}
>>> with open('commentfile.txt','r+') as file:
...     for line in file:
...        if not line.strip().startswith('#'):
...             print line,
...             file.write(line)
...
define host {
        use             template
        host_name       google_linux
        address         192.168.0.12
}

>>> with open('commentfile.txt','r+') as file:
...     for line in file:
...        if line.strip().startswith('#'):
...             continue
...        print line,
...        file.write(line)
...
define host {
        use             template
        host_name       google_linux
        address         192.168.0.12
}
cat commentfile.txt
#Hi this the comment
    define host {
            use             template
            host_name       google_linux
            address         192.168.0.12
    }

    #commented config
    #ddefine host {
    #d      use             template1
    #d      host_name       fb_linux
    #d      address         192.168.0.13
    #d}
 cat commentfile.txt
 define host {
                use             template
                host_name       google_linux
                address         192.168.0.12
        }
for line in file:
           m = re.match(r'^([^#]*)#(.*)$', line)
           if m:
              continue
我甚至尝试过正则表达式方法,但无法在同一个文件上编写

RE方法:

#Hi this the comment
define host {
        use             template
        host_name       google_linux
        address         192.168.0.12
}

#commented config
#ddefine host {
#d      use             template1
#d      host_name       fb_linux
#d      address         192.168.0.13
#d}
>>> with open('commentfile.txt','r+') as file:
...     for line in file:
...        if not line.strip().startswith('#'):
...             print line,
...             file.write(line)
...
define host {
        use             template
        host_name       google_linux
        address         192.168.0.12
}

>>> with open('commentfile.txt','r+') as file:
...     for line in file:
...        if line.strip().startswith('#'):
...             continue
...        print line,
...        file.write(line)
...
define host {
        use             template
        host_name       google_linux
        address         192.168.0.12
}
cat commentfile.txt
#Hi this the comment
    define host {
            use             template
            host_name       google_linux
            address         192.168.0.12
    }

    #commented config
    #ddefine host {
    #d      use             template1
    #d      host_name       fb_linux
    #d      address         192.168.0.13
    #d}
 cat commentfile.txt
 define host {
                use             template
                host_name       google_linux
                address         192.168.0.12
        }
for line in file:
           m = re.match(r'^([^#]*)#(.*)$', line)
           if m:
              continue

任何提示都会有帮助吗?

我认为您不能将行写入正在循环的文件,您需要将行写入另一个文件,循环后可以将其移到原始文件上


或者,您可以将所有行读取到内存中,关闭并重新打开文件,然后用新处理的行写入这些行

我认为您无法将行写入正在循环的文件,您需要将其写入另一个文件,在循环后可以移动到原始文件

或者,您可以将所有行读入内存,关闭并重新打开文件,并使用新处理的行写入这些行

一些伪代码

以读取方式打开文件 在写模式下打开一个新文件,称之为temp 循环并执行一些操作(删除注释、添加任何需要添加的内容)并写入临时文件 关闭原始文件并将其删除 然后将临时文件重命名为旧文件 那就好像

fileVar = open(file, 'r')
tempFile = open(file, 'w')
for line in file:
     # do your operations
     #you could write at the same time 
     tempFile.write(linePostOperations)
fileVar.close()
os.remove(filePath)
tempFile.close()
os.rename(tempFileName, newFileName)
一些伪代码

以读取方式打开文件 在写模式下打开一个新文件,称之为temp 循环并执行一些操作(删除注释、添加任何需要添加的内容)并写入临时文件 关闭原始文件并将其删除 然后将临时文件重命名为旧文件 那就好像

fileVar = open(file, 'r')
tempFile = open(file, 'w')
for line in file:
     # do your operations
     #you could write at the same time 
     tempFile.write(linePostOperations)
fileVar.close()
os.remove(filePath)
tempFile.close()
os.rename(tempFileName, newFileName)

在读取文件时写入文件是一件非常棘手的事情。最好将整个文件读入内存,对其进行操作,然后一次性将其转储回文件中。它没有那么大,所以占用的内存无关紧要。如果它是一个小文件,请将整个文件读入内存,关闭文件,进行处理并将其写回。如果是一个大文件,则在写入另一个文件时读取该文件,然后处理,完成后,将临时文件重命名为原始文件。@deceze我正在循环这些文件。但是有些文件也有2GB的配置数据,可以吗?如果我按照你的方法。不,你不想把2GB的数据读入内存(除非你确定你可以腾出2GB的RAM)。请参阅上面@Vatine的建议。您真的需要用Python来实现这一点吗?为什么不直接使用sed呢?在读取文件时写入文件是非常棘手的。最好将整个文件读入内存,对其进行操作,然后一次性将其转储回文件中。它没有那么大,所以占用的内存无关紧要。如果它是一个小文件,请将整个文件读入内存,关闭文件,进行处理并将其写回。如果是一个大文件,则在写入另一个文件时读取该文件,然后处理,完成后,将临时文件重命名为原始文件。@deceze我正在循环这些文件。但是有些文件也有2GB的配置数据,可以吗?如果我按照你的方法。不,你不想把2GB的数据读入内存(除非你确定你可以腾出2GB的RAM)。请参阅上面@Vatine的建议。您真的需要用Python来实现这一点吗?为什么不直接用sed呢?