docs = open("sample.pdf", "r") doc_list = [] for file in docs: print('file - type : ', file.type) if file.type == 'text/plain': # file is .txt print("txt") elif file.type in ['application/octet-stream', 'application/pdf']: # file is .pdf print("pdf") elif file.type == 'text/csv': # file is .csv print("csv") elif file.type == 'application/json': # file is .json print("json")