将文件从屏幕截图移动到其他文件夹

2024-10-03 09:17:36 发布

您现在位置:Python中文网/ 问答频道 /正文

这是代码,它通过屏幕截图进行搜索,并获取具有特定字符串的截图

import os
import shutil
#from pathlib import Path


#'E:\\Test'
#if src = 'path/to/file.txt'
#grabs = 'C:\\Users\\Gal\\Videos\\Captures'
#'C:\\Users\\Gal\\Videos\\Captures'


directory = ('C:\\Users\\Gal\\Videos\\Captures')
target = ('C:\\zorko')
destination = 'zorko'


for filename in os.listdir(directory):
    data = str(filename)
    final = 'teorija' in data
    path = os.path.abspath(filename)
    if final is True:
        print('1')
        shutil.move(filename, target)

但是,当尝试使用shutil移动它们时,会显示以下错误:

Traceback (most recent call last):
  File "C:\Users\Gal\AppData\Local\Programs\Python\Python38-32\lib\shutil.py", line 788, in move
    os.rename(src, real_dst)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'T1A INF teorija _ Microsoft Teams 10_21_2020 10_39_27 AM.png' -> 'C:\\zorko\\T1A INF teorija _ Microsoft Teams 10_21_2020 10_39_27 AM.png'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Gal/PycharmProjects/pythonProject2/main.py", line 23, in <module>
    shutil.move(filename, target)
  File "C:\Users\Gal\AppData\Local\Programs\Python\Python38-32\lib\shutil.py", line 802, in move
    copy_function(src, real_dst)
  File "C:\Users\Gal\AppData\Local\Programs\Python\Python38-32\lib\shutil.py", line 432, in copy2
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "C:\Users\Gal\AppData\Local\Programs\Python\Python38-32\lib\shutil.py", line 261, in copyfile
    with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
FileNotFoundError: [Errno 2] No such file or directory: 'T1A INF teorija _ Microsoft Teams 10_21_2020 10_39_27 AM.png'

Tags: inpysrcmoveoslocallinefilename
1条回答
网友
1楼 · 发布于 2024-10-03 09:17:36
import os
import shutil

directory = ('C:\\Users\\Gal\\Videos\\Captures')
target = ('C:\\zorko')
destination = 'zorko'
for filename in os.listdir(directory):
    if 'teorija' in filename:
        shutil.move(os.path.join(directory, filename), os.path.join(target, filename))

相关问题 更多 >