Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


List files without extension

hovanhovan Posts: 1Member
I'm fairly new to python, and I am trying to write a script that will write all the files in a directory to a text file, but only write the files that have a certain extension (.avi, .rmvb, .mkv etc.) and only print the filename without the extension. So video.avi would become video in the text file, and text.txt would be ignored completely. So far I have been able to list the directory using os.listdir, and write it to a text file, but haven't been able to show only certain extensions and show only the filename. What would be the best way of doing this?


  • cpoyntercpoynter Posts: 2Member

    Just found your entry using a web search. I am trying to do something similar and was wondering if you could forward me your code, as I am trying to list CSV files within a directory.


  • GTSoftwareGTSoftware Posts: 2Member
    Hopefully you've worked it out by now (python is great for experimentation in the python shell) but just in case..(and remember that spaces for indentation matter in python)..

    [code]# import os so you get the os access methods
    import os

    # set a directory you're interested in
    workingDir = r'mydirectory'

    # get a list of all the files in the directory
    names = os.listdir(workingDir)

    # look at each file and decide if you like it (say, ends '.ABC')
    for name in names:
    fileExt = os.path.splitext(name)[-1]
    if '.ABC' == fileExt:
    print name

    # the same, but looking for filenames without an extension
    for name in names:
    fileExt = os.path.splitext(name)[-1]
    if '' == fileExt:
    print name

Sign In or Register to comment.