Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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?

Comments

  • cpoyntercpoynter Posts: 2Member
    Hi,

    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.

    Thanks,

    Craig
  • 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
    [/code]



Sign In or Register to comment.