小编use*_*916的帖子

如果python中已存在相同的文件名,则移动并替换

以下是将移动和替换单个文件的代码

import shutil
import os
src = 'scrFolder'
dst = './dstFolder/'
filelist = []

files = os.listdir( src )
for filename in files:
 filelist.append(filename)
 fullpath = src + '/' + filename
 shutil.move(fullpath, dst)
Run Code Online (Sandbox Code Playgroud)

如果我执行相同的命令并移动已经存在的文件dst folder我正在获取shutil.Error: Destination path './dstFolder/file.txt' already exists如何移动并替换相同的文件名已经存在

shutil python-2.7

36
推荐指数
2
解决办法
6万
查看次数

查找图像 rgb 像素颜色计数的最快方法

我有一个用例,我必须在搜索后找到实时视频每一帧的连续 rgb 像素颜色计数,我发现了一段代码,它做同样的事情,但性能方面需要大约 3 秒才能给我输出,但在在我的情况下,我必须尽快进行此计算,可能是 1 秒内 25 帧。有人可以通过重构以下代码来帮助我弄清楚如何做到这一点

from PIL import Image
import timeit

starttime = timeit.default_timer()
with Image.open("netflix.png") as image:
    color_count = {}
    width, height = image.size
    print(width,height)
    rgb_image = image.convert('RGB')
    for x in range(width):
        for y in range(height):
            rgb = rgb_image.getpixel((x, y))
            if rgb in color_count:
                color_count[rgb] += 1
            else:
                color_count[rgb] = 1

    print('Pixel Count per Unique Color:')
    print('-' * 30)
    print(len(color_count.items()))
print("The time difference is :", timeit.default_timer() - starttime)
Run Code Online (Sandbox Code Playgroud)

输出:

每个唯一颜色的像素数:130869

时差为:3.9660612

python-imaging-library opencv-python

4
推荐指数
1
解决办法
3787
查看次数

实现java 1.2中的replaceAll功能

我有以下代码

import java.io.*;

public class Test{
public static void main(String args[]){
  String Str = new String("Welcome to java world !");

  System.out.print("Return Value :" );
  System.out.println(Str.replaceAll(" ",
                     "%20" ));
}
}
Run Code Online (Sandbox Code Playgroud)

这会产生以下结果:

Return Value :Welcome%20to%20java%20world%20!
Run Code Online (Sandbox Code Playgroud)

但问题是我在我们的项目中使用遗留的java 1.2,不支持String类中的replaceAll或StringBuffer类中的replace.如何在java 1.2中实现replaceAll逻辑以用%20替换所有空间

java

3
推荐指数
1
解决办法
230
查看次数