Code: (using Os.listdir)
Import Osdef Listfilestotxt (dir,file,wildcard,recursion): exts = Wildcard.split ("") files = Os.listdir (dir) For name in Files: fullname=os.path.join (Dir,name) if (Os.path.isdir (FullName) & recursion): Listfilestotxt (fullname,file,wildcard,recursion) else: for ext in exts: if (name.endswith (EXT)): File.write (name + "\ n") breakdef Test (): dir= "j:\\1" outfile= "binaries.txt" wildcard = ". txt. EXE. dll. lib " file = open (outfile," w ") if not file: print (" Cannot open the file%s for writing "% outfile ) Listfilestotxt (Dir,file,wildcard, 1) file.close () Test ()
Code: (using Os.walk) walk recursively to the directory and subdirectory processing, each return three items are: The current recursive directory, all subdirectories under the current recursive directory, all the files in the current recursive directory.
import osdef listfilestotxt (dir,file,wildcard,recursion): exts = Wildcard.split ("") for root, subdirs, files in Os.walk (dir): for name in files:for ext in exts: if (Name.endswith (EXT)): File.write (name + "\ n") break if (not RECU rsion): Breakdef Test (): dir= "j:\\1" outfile= "binaries.txt" wildcard = ". txt. exe. dll. lib" File = open ( OutFile, "W") If not file:print ("Cannot open the file%s for writing"% outfile) listfilestotxt (dir,file,wildcard, 0 ) File.close () Test ()