该脚本应递归遍历根路径目录并查找扩展名为*.mp4的所有文件.使用目录结构打印文件列表.然后将文件移动到destDir目录.我遇到的问题是尝试将文件移动到新目录时.只有rootPath目录中的文件才会移动到新目标.rootPath下子目录中的文件会导致错误:
/Volumes/VoigtKampff/Temp/TEST/level01_test.mp4
/Volumes/VoigtKampff/Temp/TEST/Destination/2levelstest02.mp4
Traceback (most recent call last):
File "/Volumes/HomeFolders/idmo04/Desktop/ScriptsLibrary/Python/recursive_find.py", line 14, in <module>
shutil.move(root+filename, destDir+'/'+filename)
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/shutil.py", line 281, in move
copy2(src, real_dst)
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/shutil.py", line 110, in copy2
copyfile(src, dst)
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/shutil.py", line 65, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: '/Volumes/VoigtKampff/Temp/TEST/Destination2levelstest02.mp4'
Run Code Online (Sandbox Code Playgroud)
##############这是脚本
import fnmatch
import os
import shutil
rootPath = '/Volumes/VoigtKampff/Temp/TEST/'
destDir = '/Volumes/VoigtKampff/Temp/TEST2/'
matches = []
for root, dirnames, filenames in os.walk(rootPath):
for filename in fnmatch.filter(filenames, '*.mp4'):
matches.append(os.path.join(root, …Run Code Online (Sandbox Code Playgroud) 我们的存储区域遇到了SMB连接问题,现在我们被迫定期使用FTP访问文件。因此,我没有使用Bash,而是尝试使用python,但遇到了一些问题。该脚本需要递归搜索FTP目录,并查找所有24小时以后的文件“ * 1700_m30.mp4”。然后在本地复制所有这些文件。
这是到目前为止的内容-但是我似乎无法获取脚本来下载文件或从文件中获取统计信息,这些信息告诉我它们是否比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 …Run Code Online (Sandbox Code Playgroud) 我正在尝试扫描文件系统中的文件 - 如果文件位于数据库中,则更新。如果没有则插入一条新记录。但我收到此错误“TypeError update() 获得了参数‘upsert’的多个值”
我在这里做错了什么?
def scan_for_file(dir_path, storage_bucket):
for file in os.listdir(dir_path):
curpath = os.path.join(dir_path, file)
if os.path.isfile(curpath):
print('found file:', file)
print('checking db')
collection = db.file_collection
cursor = collection.find({'file_path': curpath})
for document in cursor:
file_db_id = document['_id']
file_accessed = datetime.datetime.fromtimestamp(os.path.getatime(curpath))
file_modified = datetime.datetime.fromtimestamp(os.path.getmtime(curpath))
file_created = datetime.datetime.fromtimestamp(os.path.getctime(curpath))
file_size = os.stat(curpath).st_size
file_convert_size = convert_size(file_size)
utc_datetime = datetime.datetime.utcnow()
update_id = collection.update_one({'_id': file_db_id}, {
"$set": {
"file_size": file_convert_size,
"file_size_bytes": file_size,
"process_datestamp": utc_datetime.strftime("%Y-%m-%d %H:%M:%S"),
"file_created": file_created,
"file_accessed": file_accessed,
"file_modified": file_modified}},
{
"$setOnInsert": {
"file_path": …Run Code Online (Sandbox Code Playgroud)