How to only get files without extensions?

Question:

My problem is to get ONLY files without extensions.
I mean – I have a dictionary and there are some files without extensions and some files with extensions (.xml, .csv, etc)

I want that my code would only read files without extensions.

Now, it’s reading every file in the dictionary "Dir".

path = 'C:/Users/STJ2TW/Desktop/Dir/'
for filename in os.listdir(path):
    fullname = os.path.join(path, filename)

Thanks in advance!

Asked By: ASAS

||

Answers:

If there are no dots in your files, you can do :

path = 'C:/Users/STJ2TW/Desktop/Dir/'
for filename in os.listdir(path):
    if '.' not in filename:
        fullname = os.path.join(path, filename)
            
Answered By: Said Amz

You can split the filename using the splittext function and check for the ones which are not a directory and do not have an extension value (ext).

import os
path = os.getcwd()
for filename in os.listdir(path):
    if not os.path.isdir(filename): 
        (name, ext) = os.path.splitext(filename)
        if not ext:
            # Your code here
Answered By: Darshil Jani

Adding another if loop works for me:

    if '.' not in filename:
        fullname = os.path.join(path, filename)
Answered By: ASAS
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.