gpt4 book ai didi

用于递归搜索 FTP 以查找特定文件名且更新时间超过 24 小时的 python 脚本

转载 作者:行者123 更新时间:2023-11-28 22:02:28 24 4
gpt4 key购买 nike

我们的存储区域遇到了 SMB 连接问题,现在我们不得不定期使用 FTP 访问文件。因此,我没有使用 Bash,而是尝试使用 python,但遇到了一些问题。该脚本需要递归搜索 FTP 目录并找到所有文件 "*1700_m30.mp4"更新时间超过 24 小时。然后将所有这些文件复制到本地。

这就是我到目前为止所得到的 - 但我似乎无法获取脚本来下载文件或从文件中获取统计信息来告诉我它们是否比 24 小时新。

#!/usr/bin/env python
# encoding: utf-8

import sys
import os
import ftplib
import ftputil
import fnmatch
import time

dir_dest = '/Volumes/VoigtKampff/Temp/TEST1/' # Directory where the files needs to be downloaded to
pattern = '*1700_m30.mp4' #filename pattern for what the script is looking for
print 'Looking for this pattern :', pattern # print pattern


print "logging into GSP" # print
host = ftputil.FTPHost('xxx.xxx','xxx','xxxxx') # ftp host info
recursive = host.walk("/GSPstor/xxxxx/xxx/xxx/xxx/xxxx",topdown=True,onerror=None) # recursive search
for root,dirs,files in recursive:
for name in files:
print 'Files :', files # print all files it finds
video_list = fnmatch.filter(files, pattern)
print 'Files to be moved :', video_list # print list of files to be moved
if host.path.isfile(video_list): # check whether the file is valid
host.download(video_list, video_list, 'b') # download file list



host.close

这是根据 ottomeister 的优秀建议修改后的脚本(谢谢!!) - 现在的最后一个问题是它下载但它不断下载文件并覆盖现有文件:

import sys
import os
import ftplib
import ftputil
import fnmatch
import time
from time import mktime
import datetime
import os.path, time
from ftplib import FTP


dir_dest = '/Volumes/VoigtKampff/Temp/TEST1/' # Directory where the files needs to be downloaded to
pattern = '*1700_m30.mp4' #filename pattern for what the script is looking for
print 'Looking for this pattern :', pattern # print pattern
utc_datetime_less24H = datetime.datetime.utcnow()-datetime.timedelta(seconds=86400) #UTC time minus 24 hours in seconds
print 'UTC time less than 24 Hours is: ', utc_datetime_less24H.strftime("%Y-%m-%d %H:%M:%S") # print UTC time minus 24 hours in seconds
print "logging into GSP FTP" # print


with ftputil.FTPHost('xxxxxxxx','xxxxxx','xxxxxx') as host: # ftp host info
recursive = host.walk("/GSPstor/xxxx/com/xxxx/xxxx/xxxxxx",topdown=True,onerror=None) # recursive search
for root,dirs,files in recursive:
for name in files:
print 'Files :', files # print all files it finds
video_list = fnmatch.filter(files, pattern) # collect all files that match pattern into variable:video_list
statinfo = host.stat(root, video_list) # get the stats from files in variable:video_list
file_mtime = datetime.datetime.utcfromtimestamp(statinfo.st_mtime)
print 'Files with pattern: %s and epoch mtime is: %s ' % (video_list, statinfo.st_mtime)
print 'Last Modified: %s' % datetime.datetime.utcfromtimestamp(statinfo.st_mtime)
if file_mtime >= utc_datetime_less24H:
for fname in video_list:
fpath = host.path.join(root, fname)
if host.path.isfile(fpath):
host.download_if_newer(fpath, os.path.join(dir_dest, fname), 'b')

host.close()

最佳答案

这一行:

    video_list = fnmatch.filter(files, pattern)

为您提供与您的 glob 模式匹配的文件名列表。但是这一行:

    if host.path.isfile(video_list): # check whether the file is valid 

是伪造的,因为 host.path.isfile() 不希望将文件名列表作为其参数。它需要一个单一的路径名。因此,您需要遍历 video_list,一次构造一个路径名,将这些路径名中的每一个传递给 host.path.isfile(),然后可能会下载该特定文件。像这样:

    import os.path

for fname in video_list:
fpath = host.path.join(root, fname)
if host.path.isfile(fpath):
host.download(fpath, os.path.join(dir_dest, fname), 'b')

请注意,我正在使用 host.path.join() 来管理远程路径名,并使用 os.path.join() 来管理本地路径名。另请注意,这会将所有下载的文件放入一个目录中。如果你想把它们放到一个镜像远程布局的目录层次结构中(如果不同远程目录中的文件名可能发生冲突,你将不得不做类似的事情)那么你需要构建一个不同的目标路径,并且你'您可能还必须创建本地目标目录层次结构。

要获取时间戳信息,请使用 host.lstat()host.stat(),具体取决于您希望如何处理符号链接(symbolic link)。

是的,应该是 host.close()。没有它,连接将在 host 变量超出范围并被垃圾收集后关闭,但最好明确关闭它。更好的是,使用 with 子句来确保连接被关闭,即使异常导致此代码在到达 host.close() 调用之前被放弃,例如这个:

    with ftputil.FTPHost('xxx.xxx','xxx','xxxxx') as host: # ftp host info
recursive = host.walk(...)
...

关于用于递归搜索 FTP 以查找特定文件名且更新时间超过 24 小时的 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11209302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com