Pylint给我"最后的新线缺失"

Ner*_*kic 11 python pylint python-3.x

Pylint在我调用函数"deletdcmfiles()"的最后一行抱怨."最后的换行线丢失".我是python的新手,我不确定是什么触发了这个?

这是程序的代码:

'''
This program will go through all Work subdirectorys in "D:\\Archvies" folder
and delete all DCM files older then three months.
'''
import os.path
import glob
import time

#Create a list of Work directorys in Archive folder
WORKDIR = glob.glob("D:\\Archives\\ASP*\\Work*")
#Variable holds three months of time in seconds
THREEMONTHSOLD = (time.time()) - (90 * 86400)

def deletdcmfiles():
    '''
    This function will go through all Work subdirectorys in "D:\\Archvies" folder
    and delete all DCM files older then three months.
    '''
    #Variable to keep counter of deleted files
    deleted_files = 0
    #Loop through each Work subdirectory and delete all .DCM files older then 3 months
    for mydir in enumerate(WORKDIR):
        #Store all directory files in a list
        dcmfiles = glob.glob(mydir[1] + "\\" + "*.dcm")
        #Compare Modified Date of each DCM file and delete if older then 3 Months
        for file in enumerate(dcmfiles):
            if os.path.getmtime(file[1]) < THREEMONTHSOLD:
                print("Deleted " + file[1] + " " + time.ctime(os.path.getmtime(file[1])))
                os.remove(file[1])
                deleted_files += 1
    print("Total Files Deleted :" + str(deleted_files))

#Run program
deletdcmfiles()
Run Code Online (Sandbox Code Playgroud)

Mur*_*nik 19

您需要在文件末尾添加一个空行.只需ENTER在最后一行的末尾添加另一个,你就可以了.

  • 这是为什么呢? (4认同)

小智 10

我刚刚遇到这个问题并找到了类似问题的答案:

您至少需要一个换行符的原因是,从历史上看,如果文件结束且最后一行有文本但文件末尾没有换行符,某些工具就会出现问题。写得不好的工具可能会错过对最后一部分行的处理,或者更糟糕的是可以读取最后一行之外的随机内存(虽然用 Python 编写的工具不太可能发生这种情况,但用 C 编写的工具可能会发生)。

因此,您必须确保有一个换行符终止最后一个非空行。