python read&检查文件第一行

时间:2014-10-27 05:05:43

标签: python python-3.x

我试图找到要添加CopyRigth块的文件。 所以,我写这个:

import os
for root, dirs, files in os.walk('.'):
    for file in files:
       if file.endswith(".cs"):
           try:
               content = open(root + "\\" + file, "rt", encoding="utf-8").readlines()[0]
               if not content.startswith("#region Copy"):
                   print(content)
                   print(content.startswith("#region Copy"))
                   print(root + "\\" + file)
           except Exception as e:
               pass

可以使用,但如果注释print(content),则会打印所有文件名

import os
for root, dirs, files in os.walk('.'):
    for file in files:
       if file.endswith(".cs"):
           try:
               content = open(root + "\\" + file, "rt", encoding="utf-8").readlines()[0]
               if not content.startswith("#region Copy"):
                   # print(content)
                   print(content.startswith("#region Copy"))
                   print(root + "\\" + file)
           except Exception as e:
               pass

有这种魔力的原因吗?

0 个答案:

没有答案