os.walk()应用于订单程序

我想在我的具体情况下使用 walk.os,因为时间到了,我订购了一些图像。这些图像位于文件夹“D:/TR/Eumetsat_IR_photos/Prueba”中,我的想法是从“D:/TR/Eumetsat_IR_photos”中包含的不同文件夹中获取所有图像,将它们排序到两个名为“白天”和“夜间”的特定文件夹中。我不知道如何调整程序以使用这个 os.walk()


这并不重要,但是图像的时间出现在第 37 和 39 位的所有图像名称中(这样你就可以正确理解它)。


谢谢


from os import listdir, path, mkdir

import shutil



directorio_origen = "D:/TR/Eumetsat_IR_photos/Prueba"

directorio_destino_dia = "D:/TR/IR_Photos/Daytime"

directorio_destino_noche = "D:/TR/IR_Photos/Nighttime"


def get_hour(file_name):

    return file_name[37:39]


for fn in list0:

    hora = int(get_hour(fn))

    file_directorio= directorio_origen+"/"+fn

    if 6 < hora < 19: 

        new_directorio= directorio_destino_dia

    else:

        new_directorio= directorio_destino_noche

        

    try:

        if not path.exists(new_directorio):

            mkdir(new_directorio)

        shutil.copy(file_directorio, new_directorio)

    except OSError:

        print("el archivo %s no se ha ordenado" % fn)


Smart猫小萌
浏览 52回答 1
1回答

幕布斯7119047

通过对代码进行一些修改,类似这样的事情就可以做到:import shutilimport osdirectorio_origen = "D:/TR/Eumetsat_IR_photos/Prueba"directorio_destino_dia = "D:/TR/IR_Photos/Daytime"directorio_destino_noche = "D:/TR/IR_Photos/Nighttime"def get_hour(file_name):&nbsp; &nbsp; return file_name[37:39]for root, dirs, files in os.walk(directorio_origen, topdown=False):&nbsp; &nbsp; for fn in files:&nbsp; &nbsp; &nbsp; &nbsp; path_to_file = os.path.join(root, fn)&nbsp; &nbsp; &nbsp; &nbsp; hora = int(get_hour(fn))&nbsp; &nbsp; &nbsp; &nbsp; new_directorio = ''&nbsp; &nbsp; &nbsp; &nbsp; if 6 < hora < 19:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new_directorio= directorio_destino_dia&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new_directorio= directorio_destino_noche&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not os.path.exists(new_directorio):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; os.mkdir(new_directorio)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shutil.copy(path_to_file, new_directorio)&nbsp; &nbsp; &nbsp; &nbsp; except OSError:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("el archivo %s no se ha ordenado" % fn)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python