Pylint giving me "Final new line missing"
Asked Answered
I

3

24

Pylint complains on last line where I'm calling function deletdcmfiles(). Final newline missing. I'm new to python and I'm not sure what is triggering this?

Here is the code of the program:

'''
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()
Interclavicle answered 1/6, 2017 at 21:59 Comment(2)
It expects an additional newline character at the end of the file. It doesn't really have anything to do with the Python code contained in the file.Duress
Thank you that resolved it.Interclavicle
V
49

You need an empty new line at the end of your file. Just add another ENTER at the end of the last line and you'll be fine.

Votyak answered 1/6, 2017 at 22:2 Comment(1)
Why is that though?Rahman
H
10

I just ran into this issue and found this answer to a similar question:

The reason you need at least one newline is that historically some tools have problems if the file ends and the last line has text on it but does not have a newline at the end of the file. Badly written tools can miss processing that last partial line, or even worse can read random memory beyond the last line (though that is unlikely to happen with tools written in Python it can happen with tools written in C).

So you must ensure there is a newline terminating the last non-blank line.

Haem answered 17/1, 2021 at 16:37 Comment(0)
F
0

I got the same error below with Pylint:

Final newline missingPylint(C0304:missing-final-newline)

Because I didn't add one blank line after the last code print(math.pi) as shown below:

1 import math
2
3 print(math.pi)

So, I added one blank line after the last code print(math.pi) as shown below, then the error was solved:

1 import math
2
3 print(math.pi)
4

In addition, if you add more than one blank lines after the last code as shown below:

1 import math
2
3 print(math.pi)
4
5

Then, you get the error below:

Trailing newlinesPylint(C0305:trailing-newlines)

Fowlkes answered 10/6, 2023 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.