'How to deal with files with no extension in Python

I am currently writing a script for work, which goes through every file in a directory and its many sub directories and counts the amount of lines in each file. After its done, it writes the amount of lines and the corresponding path to the files in a csv. I want to have a textfile which specifies by fileextension, which files should be counted. The file looks like this:

.cpp
.c
.h
.bat
and so on

This works great, until there are files with no extension. They're just called "File". I tried to add a blank line to the fileextensions, but this just caused the script to take all files, even the ones not specified. Here is my code

import os


FILE_TYPES_FILE = "file_types.txt"
DIRECTORY_NAME = "directory_to_go_through"
OUTPUT_FILE_NAME = "file_lengths.csv"
OUTPUT_FILE_FOLDER = "folder.csv"
TO_SKIP_FILE = "to_skip.txt"

def get_file_length(filename):
    with open(filename, "rb") as file:
         data = file.readlines()
    return len(data)

with open(FILE_TYPES_FILE, "r") as file:
   FILE_TYPES = file.read().split("\n")
    FILE_TYPES = tuple(FILE_TYPES)


FILES_TO_SKIP = []
DIRS_TO_SKIP = []
with open(TO_SKIP_FILE, "r") as file:
    to_skip = file.read().split("\n")
    for thing_to_skip in to_skip:
        if os.path.isfile(thing_to_skip):
            FILES_TO_SKIP.append(thing_to_skip)
            print(f"{thing_to_skip} added to files-to-skip")
        elif os.path.isdir(thing_to_skip):
            DIRS_TO_SKIP.append(thing_to_skip)
            print(f"{thing_to_skip} added to directorys-to-skip")
        else:
            continue


list_of_files = []
excel_string = ""
for(dirpath, dirnames, filenames) in os.walk(DIRECTORY_NAME):
    list_of_files += [os.path.join(dirpath, file) for file in filenames if (dirpath not in DIRS_TO_SKIP) and (os.path.join(dirpath, file) not in FILES_TO_SKIP) and file.endswith(FILE_TYPES) and file.endswith(None)]
    excel_string = excel_string + dirpath+"\n"


with open(OUTPUT_FILE_FOLDER, "w") as file:
    file.write(excel_string)

input("Press enter to exit...") 
excel_string = ""


for file in list_of_files:
    excel_string += f"{file};{get_file_length(file)}\n"

print(excel_string)

with open(OUTPUT_FILE_NAME, "w") as file:
    file.write(excel_string)
    print(excel_string)


input("Press enter to exit...")

The exclusion of certain directories isn't working as it should either, but thats a different problem.

The CSV should look like this:

path/to/file1;520
path/to/file2;145
path/to/file3;412
and so on


Solution 1:[1]

You could add a blank line at the end of your file with file types, and then use os.path.splitext to get the extension of your filename and apply logic on it. Code sample:

import os.path

# This represents reading your file
FILE_TYPES_TO_USE = tuple('''.cpp
.c
.h
'''.split('\n'))
print(f'File types to use: {FILE_TYPES_TO_USE}')

# This represents going through your folder
for filename in ['1.cpp', '2.c', '3.h', 'File', '.bashrc', 'file.txt']:
    if os.path.splitext(filename)[-1] in FILE_TYPES_TO_USE:
        print(f'Do something with {filename}')
    else:
        print(f'Do nothing with {filename}')

Output will be:

File types to use: ('.cpp', '.c', '.h', '')
Do something with 1.cpp
Do something with 2.c
Do something with 3.h
Do something with File
Do something with .bashrc
Do nothing with file.txt

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 physicalattraction