0

How do i make python listen for changes to a folder on my desktop, and every time a file was added, the program would read the file name and categorize it it based on the extension?

This is a part of a more detailed program but I don't know how to get started on this part. This part of the program detects when the user drags a file into a folder on his/her desktop and then moves that file to a different location based on the file extension.

Ellipsis
  • 3
  • 1
  • I don't think this is a duplicate based on the referenced existing answer. It doesn't address the core of OP's question. – directive-41 Nov 15 '18 at 00:08

1 Answers1

0

Periodically read the files in the folder and compare to a set of files remaining after the last execution of your script. Use os.listdir() and isfile().

Read the extension of new files and copy them to a directory based on internal rules. This is a simple string slice, e.g., filename[-3:] for 3-character extensions.

Remove moved files from your set of last results. Use os.rename() or shutil.move(). Sleep until next execution is scheduled.

directive-41
  • 113
  • 10
  • An alternative to the string slice is to split on '.' and retain the last element. That would capture any length extension. – directive-41 Nov 15 '18 at 00:09