亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

os.walk()應用于訂單程序

os.walk()應用于訂單程序

Smart貓小萌 2023-12-29 10:33:02
我想在我的具體情況下使用 walk.os,因為時間到了,我訂購了一些圖像。這些圖像位于文件夾“D:/TR/Eumetsat_IR_photos/Prueba”中,我的想法是從“D:/TR/Eumetsat_IR_photos”中包含的不同文件夾中獲取所有圖像,將它們排序到兩個名為“白天”和“夜間”的特定文件夾中。我不知道如何調整程序以使用這個 os.walk()這并不重要,但是圖像的時間出現在第 37 和 39 位的所有圖像名稱中(這樣你就可以正確理解它)。謝謝from os import listdir, path, mkdirimport shutildirectorio_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)
查看完整描述

1 回答

?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

通過對代碼進行一些修改,類似這樣的事情就可以做到:


import shutil

import os


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 root, dirs, files in os.walk(directorio_origen, topdown=False):

    for fn in files:

        path_to_file = os.path.join(root, fn)

        hora = int(get_hour(fn))

        new_directorio = ''

        if 6 < hora < 19: 

            new_directorio= directorio_destino_dia

        else:

            new_directorio= directorio_destino_noche

        try:

            if not os.path.exists(new_directorio):

                os.mkdir(new_directorio)

            shutil.copy(path_to_file, new_directorio)

        except OSError:

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


查看完整回答
反對 回復 2023-12-29
  • 1 回答
  • 0 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號