Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python FileNotFoundError:[WinError 3]系统找不到指定的路径。Windows操作系统_Python_Automation - Fatal编程技术网

Python FileNotFoundError:[WinError 3]系统找不到指定的路径。Windows操作系统

Python FileNotFoundError:[WinError 3]系统找不到指定的路径。Windows操作系统,python,automation,Python,Automation,我正在尝试在我的windows笔记本电脑上部署和运行我的自动化程序。但是我遇到了一个错误。我不知道如何解决这个问题。我试着在网上搜索,但什么也没找到。我今年14岁,是Python的初学者。这是一个关于自动移动文件和文件夹的项目,这样我的笔记本电脑就可以被组织起来 $ C:/Users/siddt/python.exe c:/Users/siddt/NodeJs/auto.py Traceback (most recent call last): File "c:/Users/sid

我正在尝试在我的windows笔记本电脑上部署和运行我的自动化程序。但是我遇到了一个错误。我不知道如何解决这个问题。我试着在网上搜索,但什么也没找到。我今年14岁,是Python的初学者。这是一个关于自动移动文件和文件夹的项目,这样我的笔记本电脑就可以被组织起来

$ 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.
我非常感谢您的帮助。

试试这个:

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()
对于移动文件,我使用Pathlib中的Path(它适用于python 3.4+) 您必须替换目录。 我希望它有用

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()