Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在订单程序中应用的os.walk()_Python_Python 3.x_Os.walk - Fatal编程技术网

Python 在订单程序中应用的os.walk()

Python 在订单程序中应用的os.walk(),python,python-3.x,os.walk,Python,Python 3.x,Os.walk,我想在我的特定情况下使用walk.os,因为现在是小时,所以我订购了一些图像。这些图像位于文件夹“D:/TR/Eumetsat_IR_photos/Prueba”中,我的想法是从“D:/TR/Eumetsat_IR_photos”中包含的不同文件夹中获取所有图像,并将它们排序到两个特定的文件夹中,即白天和夜间。我不知道如何调整程序以使用此os.walk() 这并不重要,但图像的时间会出现在所有图像名称的位置37和39(因此您可以正确理解它) 谢谢 from os import listdir,

我想在我的特定情况下使用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)
从操作系统导入listdir,path,mkdir
进口舒蒂尔
directorio\u origen=“D:/TR/Eumetsat\u IR\u photos/Prueba”
directorio_destino_dia=“D:/TR/IR_照片/白天”
directorio\u destino\u noche=“D:/TR/IR\u照片/夜间”
def get_小时(文件名):
返回文件名[37:39]
对于清单0中的fn:
hora=int(获取小时(fn))
文件\u directorio=directorio\u origen+“/”+fn
如果6小时<19小时:
新董事=董事会董事
其他:
新董事
尝试:
如果路径不存在(新目录):
mkdir(新董事)
复制(文件目录、新目录)
除操作错误外:
打印(“el archivo%s无se ha ordenado”%fn)

只需对代码稍作修改,就可以执行以下操作:

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)
导入shutil
导入操作系统
directorio\u origen=“D:/TR/Eumetsat\u IR\u photos/Prueba”
directorio_destino_dia=“D:/TR/IR_照片/白天”
directorio\u destino\u noche=“D:/TR/IR\u照片/夜间”
def get_小时(文件名):
返回文件名[37:39]
对于os.walk(directorio\u origen,top-down=False)中的根目录、目录和文件:
对于文件中的fn:
path_to_file=os.path.join(根目录,fn)
hora=int(获取小时(fn))
新董事=“”
如果6小时<19小时:
新董事=董事会董事
其他:
新董事
尝试:
如果操作系统路径不存在(新目录):
os.mkdir(新董事)
复制(路径到文件,新目录)
除操作错误外:
打印(“el archivo%s无se ha ordenado”%fn)