• notice
  • Congratulations on the launch of the Sought Tech site

Use the Python built-in package pathlib to filter files of the same suffix type

Use the Python built-in package pathlib

The interactive mode is written here, and you can also customize the path parameters and pass them in

# Get all xlsx, txt and other files in the specified directory. See the specific requirements
def get_file ( patterns , path ):
   all_files =[]
   p = Path ( path )
   for item in patterns :
       file_name = p.rglob ( f ' **/*.{item} ' )
       all_files.extend ( file_name ) _ _
   return all_files

path = input ( '>>>>Please output the path of the file:' ) # You can customize the path    
need_file = get_file ([ 'xlsx' , 'txt' ], path )  
for i in need_file :
   print ( i )


Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+