Apr-22-2017, 10:35 AM
Your code works fine for me. I switched it to .txt or .py, and in prints a bunch of Trues and Falses.
It is rather inefficient. You are checking each file twice, once to see if it's a Word doc, once to see if it's a PowerPoint file. It would be better to make a dictionary with file extensions as the keys, and folders they should be copied to as the values. Also, I would check with endswith rather than in. It's more precise and (again) more efficient.
It is rather inefficient. You are checking each file twice, once to see if it's a Word doc, once to see if it's a PowerPoint file. It would be better to make a dictionary with file extensions as the keys, and folders they should be copied to as the values. Also, I would check with endswith rather than in. It's more precise and (again) more efficient.
import os FileTypes = {'.txt': 'Text file','.py': 'Python program', '.xml': 'XML data'} dirList = os.listdir(os.getcwd()) for item in dirList: for extension, folder in FileTypes.items(): if item.endswith(extension): print(folder) break else: print('Other')See how I used break? It only checks until it finds something, then it stops checking the other extensions. We can also use the else clause to catch anything to didn't match the extensions we were looking for.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures