我正在尝试创建一个名称为变量的目录,其中包含一个文件夹(也是变量)。目前,这就是我所拥有的:
### sort and copy files to backup
if event.event_type == 'created':
#modFile = event.src_path[:-1]
chainFolderOut = backupDir + event.src_path
print(chainFolderOut)
print(event.src_path)
chainFolderIn = chainFolderOut + '/' + str(timestamp)
print(chainFolderIn)
os.mkdir(chainFolderOut)
os.mkdir(chainFolderIn)
这是我在尝试运行程序时遇到的错误:
/Users/Spencer/Desktop/backupTest/Users/Spencer/Desktop/TestWatchdog\New Text Document (4).txt
/Users/Spencer/Desktop/TestWatchdog\New Text Document (4).txt
/Users/Spencer/Desktop/backupTest/Users/Spencer/Desktop/TestWatchdog\New Text Document (4).txt/1537919413.7257273
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\Spencer\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "C:\Users\Spencer\AppData\Local\Programs\Python\Python37\lib\site-packages\watchdog\observers\api.py", line 199, in run
self.dispatch_events(self.event_queue, self.timeout)
File "C:\Users\Spencer\AppData\Local\Programs\Python\Python37\lib\site-packages\watchdog\observers\api.py", line 368, in dispatch_events
handler.dispatch(event)
File "C:\Users\Spencer\AppData\Local\Programs\Python\Python37\lib\site-packages\watchdog\events.py", line 322, in dispatch
self.on_any_event(event)
File "C:\Users\Spencer\Desktop\tesseractAlpha1.0\tesseractAlpha2.0.py", line 53, in on_any_event
os.mkdir(chainFolderOut)
FileNotFoundError: [WinError 3] The system cannot find the path specified: '/Users/Spencer/Desktop/backupTest/Users/Spencer/Desktop/TestWatchdog\\New Text Document (4).txt'
我在这里做错了什么? event.src_path是我要处理的文件的完整路径的变量。我不知道最后一个斜杠来自文件名之前的位置。 最终,我创建了一个程序,该程序监视一个目录中的文件更改,然后将更改的文件复制到备份文件夹/以文件名命名的文件夹的路径>时间戳