Using the Python client, how can I recursively upload all files in a local directory to Minio while preserving the directory structure?
This recursive function uploads all files presuming Minio client is initialized beforehand:
import glob
def upload_local_directory_to_minio(local_path, bucket_name, minio_path):
assert os.path.isdir(local_path)
for local_file in glob.glob(local_path + '/**'):
local_file = local_file.replace(os.sep, "/") # Replace \ with / on Windows
if not os.path.isfile(local_file):
upload_local_directory_to_minio(
local_file, bucket_name, minio_path + "/" + os.path.basename(local_file))
else:
remote_path = os.path.join(
minio_path, local_file[1 + len(local_path):])
remote_path = remote_path.replace(
os.sep, "/") # Replace \ with / on Windows
client.fput_object(bucket_name, remote_path, local_file)
Adapted from https://stackoverflow.com/a/56870303/5964489
Related
I saved one frame from each video. I want to rename the frame as the video, but I'm not sure how.
import cv2
import os
def video2frames(video_path, output_path,f_num):
if not os.path.exists(output_path):
os.makedirs(output_path)
cap = cv2.VideoCapture(video_path)
index = 0
while cap.isOpened():
Ret, Mat = cap.read()
if Ret:
index += 1
if index != f_num:
continue
cv2.imwrite(output_path + '/' + str(index) + '.png', Mat)
else:
break
cap.release()
return
def multiple_video2frames( video_path, output_path, f_num ):
list_videos = os.listdir(video_path)
for video in list_videos:
video_base = os.path.basename(video)
input_file = video_path + '/' + video
out_path = output_path + '/' + video_base
video2frames(input_file, out_path, f_num)
return
# run all
video_path = 'videos' # all videos
output_path = 'final' # location on ur pc
multiple_video2frames( video_path, output_path, 300 )
Searched: How can I extract and save image frames from a large number of videos all at once using OpenCV Python?
I am using this script as a template to rename a file in VLC: https://github.com/surrim/vlc-delete/
The Script works as intended.
My code looks like this:
function descriptor()
return {
title = "VLC Rename";
version = "0.1";
author = "I";
shortdesc = "Rename current file";
description = [[
<h1>vlc-rename</h1>"
When you're playing a file, use VLC Rename
to rename the current file]];
}
end
function removeItem()
local id = vlc.playlist.current()
vlc.playlist.delete(id)
vlc.playlist.gotoitem(id + 1)
vlc.deactivate()
end
function activate()
local item = vlc.input.item()
local uri = item:uri()
oldFile = vlc.strings.decode_uri(string.gsub(uri, "^file:///", ""))
d = vlc.dialog( "Rename Dialog" )
d:add_label("Filename")
w = d:add_text_input(oldFile, 1, 5,200 ,30)
d:add_button("OK", click_ok)
d:show()
end
function click_ok()
local newFile = w:get_text()
vlc.msg.info("[vlc-rename] renaming: " .. oldFile .. " with " .. newFile)
if newFile ~= oldFile then
removeItem()
retval, err = os.rename(oldFile,newFile)
vlc.msg.info("[vlc-rename] end rename")
if (retval == nil) then
vlc.msg.err("[vlc-rename] fail: " .. err)
end
end
d:delete()
vlc.deactivate()
end
function deactivate()
vlc.deactivate()
end
function close()
deactivate()
end
function meta_changed()
end
This code outputs an error from the os.rename() function:
lua error: [vlc-rename] fail: [my filename] Permission denied
Regardless of elevation level.
I am using windows 10 64bit and VLC 3.03.
Since this is my first lua script I welcome any input.
I might be wrong, but maybe the file you are trying to rename is already opened elsewhere or by VLC (you said you want to rename the "current file").
I am trying to make a simple Python tool for copying all the contents from drive x to drive y, where it asks the user what the source and destination drives are.
Works great when I run it from inside Visual Studio, but when I try to run it via the command line (python.exe pythonapplication1.py), I get this error in the output:
What is your source drive letter?f
Traceback (most recent call last):
File "pythonapplication1.py", line 7, in <module>
inputSrc = input("What is your source drive letter?")
File "<string>", line 1, in <module>
NameError: name 'f' is not defined
Here is my code for this program:
import os
import sys
inputSrc ="x"
inputDest = "y"
inputSrc = input("What is your source drive
letter?")
inputDest = input("What is the destination drive
letter?")
src = inputSrc + ": "
dest = inputDest + ": "
copyCommand = "xcopy " + src + dest + "/s"
os.system(copyCommand)
loopCheck = "no"
while loopCheck == "no":
questionTest = input("Want to make another copy? y/n ")
if questionTest == "y":
input("Press any key once you put in the new blank drive.")
os.system(copyCommand)
if questionTest == "n":
loopCheck = "yes"
You should use raw_input("question...") instead of input("question...") in python2. This is because input will take the users input and execute it. This is why the interpreter complains about not knowing f.
You can use raw_input() instead of input()
i.e.
inputSrc = raw_input("What is your source drive
letter?")
inputDest = raw_input("What is the destination drive
letter?")
will work.
I'm trying to determine if one of several directories are present from within a lua script. It works on OSX, but not on Windows (linux is currently untested, but I expect that one to work). When the following code runs, I get an error:
failed with this C:\Program Files (x86)\VideoLAN\VLC\lua\playlist\: No such file or directory
I can confirm that that directory exists. I've escaped the slashes, I'm not sure what else could be the issue.
local oses = { "/Applications/VLC.app/Contents/MacOS/share/lua/playlist/"; "C:\\Program Files\\VideoLAN\\VLC\\lua\\playlist\\"; "C:\\Program Files (x86)\\VideoLAN\\VLC\\lua\\playlist\\"; "/usr/lib/vlc/lua/playlist" }
-- Determine which OS this is (and where to find share/lua).
local f,err = io.open( oses[1], "r")
if not err then
opsys = "OSX"
scriptpath = oses[1] .. script
f:close()
else
f,err = io.open( oses[2], "r")
if not err then
opsys = "Win32"
scriptpath = oses[2] .. script
f:close()
else
f,err = io.open( oses[3], "r")
vlc.msg.dbg( dhead .. 'failed with this ' .. err .. dtail )
if not err then
opsys = "Win64"
scriptpath = oses[3] .. script
f:close()
else
f,err = io.open( oses[4], "r")
if not err then
opsys = "Linux/Unix"
scriptpath = oses[4] .. script
f:close()
else
return false
end
end
end
end
The file "C:\Program Files\VideoLAN\VLC\lua\playlist\" does not exist. If you were to remove the trailing slash, you'd be trying to open a directory and probably get a permissions error. It's not going to work either way. If you're going to use this method of determining OS, you should be trying to open files.
For instance, build your script path, try to open that file, and use that to determine pass/fail.
Side note, the structure of your code could be vastly improved. Any time you have a bunch of duplicate code that differs by an index, you should be using a loop. For instance, we can replace your code with this:
local oses = {
["OSX"] = "/Applications/VLC.app/Contents/MacOS/share/lua/playlist/",
["Win32"] = "C:\\Program Files\\VideoLAN\\VLC\\lua\\playlist\\",
["Win64"] = "C:\\Program Files (x86)\\VideoLAN\\VLC\\lua\\playlist\\",
["Linux/Unix"] = "/usr/lib/vlc/lua/playlist",
}
for osname, directory in pairs(oses) do
local scriptpath = directory..script
local f,err = io.open( scriptpath, "r")
if not err then
f:close()
return scriptpath, osname
end
end
I'm deploying a Windows service with a Ruby script. After copying files to the server using FileUtils.cp, I run sc \\MYSERVER start MyService via Ruby's cmd syntax. This command returns the following error for each of 20 consecutive attempts, at five second intervals:
[SC] StartService FAILED 32:
The process cannot access the file because it is being used by another process.
If I run the command manually immediately after my Ruby script ends, it works fine:
SERVICE_NAME: MyService
TYPE : 10 WIN32_OWN_PROCESS
STATE : 2 START_PENDING
(STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
PID : 21736
FLAGS :
Is FileUtils.cp possibly putting a lock on the copied EXE? If not, what else in my script could possibly be holding this lock?
Here's pretty much what my script looks like, without all the automatic retry code and configuration:
srcRoot = Pathname.new 'c:\\MyService'
destRoot = Pathname.new '\\\\MYSERVER\\services\\MyService'
destRoot.each_entry() {|item|
if not %w(. ..).include?( item.to_s )
FileUtils.rm_r destRoot.to_s + "\\" + item.to_s, :force => true
end
}
destRoot.mkdir unless destRoot.exist?
for dir in %w(Release)
copy(src_root + dir + ".", destRoot) { destRoot + dir }
end
`sc \\\\MYSERVER start MyService`
Here's the copy function, which recursively copies directories and files:
# recursively copies the given source file or directory to the given destination directory.
def copy( src, destDir )
src = Pathname.new src
destDir = Pathname.new destDir
destDir.mkdir unless destDir.exist?
exclusions = %w(. .. .svn _svn Thumbs.db)
for item in Dir.glob( src + "*" )
itemPath = Pathname.new item
if not %w(. .. .svn _svn Thumbs.db).include?( itemPath.basename.to_s )
if itemPath.directory?
copy( itemPath, destDir + itemPath.basename ) {destDir + itemPath.basename}
elsif exclusions.select {|k,v| extension? k}.select {|k,v| item.include? k}.empty?
begin
FileUtils.cp( itemPath, destDir, {:verbose => true, :preserve => true} )
rescue
puts "Warning! " + $!
end
end
end
end
end
I found the issue. The MyService.exe.config file was being copied out in a separate method, which did some manipulation of the source file content before creating a new file on the server. The new file was not being closed, so attempting to start the service failed when it couldn't get a lock on the config file.