suxdavide
suxdavide

Reputation: 133

open all files without an extension - Python

Hello I'm trying to write a script to open all files without an extension in a folder organised in subfolders etc.

I've failed at finding a way to declare all files without a certain known type (there are some PNGs and txt but I'd like to understand how to generalise this approach)

Folder hierarchy:

main
-sub
--file1
--file2.txt
-sub2
--file1
--file2.png

Etc (obviously the files aren't the same in the different folders)

Example of a said file: 1.2.840.113619.2.227.20792477682.2116111104093220.75

Upvotes: 0

Views: 639

Answers (3)

Michał Machnicki
Michał Machnicki

Reputation: 2927

This will be rather general approach for the folder structure you provided:

import os

base_pth = 'path/to/you/main'  # e.g. 'C:\my\main'
known_extensions = ['.txt', '.png']

for base, dirs, _ in os.walk(base_pth):
    for d in dirs:
        for f in os.listdir(os.path.join(base, d)):
            pth = os.path.join(base, d, f)
            if os.path.isfile(pth) and os.path.splitext(f)[1] not in known_extensions:
                with open(pth, 'rb') as file:
                    # do your thing

Upvotes: 0

Ninad Gaikwad
Ninad Gaikwad

Reputation: 4490

lst = ['png', 'jpg', 'avi']

filename = '1.2.840.113619.2.227.20792477682.2116111104093220.75'
print(filename.split('.')[-1])

if any(i for i in lst if i == filename.split('.')[-1]):
    print('known extension')
else:
    print('Unknown extension')

Try this code inside your script. You can add other known file extensions to the list. Can find all known extensions list online if you need it.

Upvotes: 1

Sharku
Sharku

Reputation: 1092

os.path.splitext(path)

Split the pathname path into a pair (root, ext) such that root + ext == path, and ext is empty or begins with a period and contains at most one period. Leading periods on the basename are ignored; splitext('.cshrc') returns ('.cshrc', '').

With that you can get the extension and than you can do if ext == "" to get the files without extension.

Upvotes: 1

Related Questions