Working with files

Question

How to separate all files with a specific extension(.csv, .txt) in a directory using python?

in progress 0
Priyabrata 4 years 1 Answer 750 views Master 0

Answer ( 1 )

  1. Method 1
    import os
    for root, dirs, files in os.walk(directory):
    for file in files:
    if file.endswith(‘.txt’):
    print file

    Method 2
    import os
    path = ‘mypath/path’
    files = os.listdir(path)
    files_txt = [i for i in files if i.endswith(‘.txt’)]

Leave an answer

Browse
Browse