I am try to deploy and run my automation program on my windows laptop. But I ran into an error. I don't know how to figure out the problem. I tried searching on the internet, but I didn't find anything. I am 14 years old and I am a beginner in Python. It is a project about automatically moving files and folders so my laptop can be organized.
$ C:/Users/siddt/python.exe c:/Users/siddt/NodeJs/auto.py
Traceback (most recent call last):
File "c:/Users/siddt/NodeJs/auto.py", line 32, in <module>
observer.start()
File "C:\Users\siddt\lib\site-packages\watchdog\observers\api.py", line 260, in start
emitter.start()
File "C:\Users\siddt\lib\site-packages\watchdog\utils\__init__.py", line 110, in start
self.on_thread_start()
File "C:\Users\siddt\lib\site-packages\watchdog\observers\read_directory_changes.py", line 66, in on_thread_start
self._handle = get_directory_handle(self.watch.path)
File "C:\Users\siddt\lib\site-packages\watchdog\observers\winapi.py", line 307, in get_directory_handle
return CreateFileW(path, FILE_LIST_DIRECTORY, WATCHDOG_FILE_SHARE_FLAGS,
File "C:\Users\siddt\lib\site-packages\watchdog\observers\winapi.py", line 113, in _errcheck_handle
raise ctypes.WinError()
FileNotFoundError: [WinError 3] The system cannot find the path specified.
Here is the code to my program.
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import os
import json
import time
class myHandler(FileSystemEventHandler):
i = 1
def on_modified(self, event):
new_name = "new_file_" + str(self.i) + ".txt"
for filename in os.listdir(folder_to_track):
file_exists = os.path.isfile(folder_destination + "/" + new_name)
while file_exists:
self.i += 1
new_name = "new_file_" + str(self.i) + ".txt"
file_exists = os.path.isfile(
folder_destination + "/" + new_name)
src = folder_to_track + "/" + filename
new_destination = folder_destination + "/" + new_name
os.rename(src, new_destination)
folder_to_track = '/Users/Desktop/myFolder'
folder_destination = '/Users/Desktop/newFolder'
event_handler = myHandler()
observer = Observer()
observer.schedule(event_handler, folder_to_track, recursive=True)
observer.start()
try:
while True:
time.sleep(10)
except KeyboardInterrupt:
observer.stop()
observer.join()
I would really appreciate some help.
Just try this:
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler, PatternMatchingEventHandler
import os
import time
from pathlib import Path
def on_modified(event):
for filename in os.listdir(folder_to_track):
print('Checking file {}....'.format(filename))
chk_name = folder_to_track + "/" + filename
if os.path.isfile(folder_destination + "/" + filename):
print('The file {} exists'.format(filename))
else:
newfile = os.path.join(folder_destination, os.path.basename(filename))
print (chk_name)
print (newfile)
Path(chk_name).rename(newfile)
print('File {} moved'.format(filename))
folder_to_track = '/YOUR START PATH'
folder_destination = '/YOUR DESTINATION PATH'
if __name__ == "__main__":
patterns = "*"
ignore_patterns = ""
ignore_directories = False
case_sensitive = True
my_event_handler = PatternMatchingEventHandler(patterns, ignore_patterns, ignore_directories, case_sensitive)
my_event_handler.on_modified = on_modified
go_recursively = True
observer = Observer()
observer.schedule(my_event_handler, folder_to_track, recursive=go_recursively)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
For moving files I use Path from Pathlib (it works on python 3.4+) You must replace the directory. I hope it's useful!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.