Python: How can I find all files with a particular extension?

前端 未结 13 1980
广开言路
广开言路 2020-12-13 14:05

I am trying to find all the .c files in a directory using Python.

I wrote this, but it is just returning me all files - not just .c files.

13条回答
  •  情话喂你
    2020-12-13 14:46

    Change the directory to the given path, so that you can search files within directory. If you don't change the directory then this code will search files in your present directory location:

    import os  #importing os library 
    import glob #importing glob library
    
    path=raw_input()  #input from the user 
    os.chdir(path)
    
    filedata=glob.glob('*.c') #all files with .c extenstions stores in filedata.
    print filedata
    

提交回复
热议问题