导入并读取文件夹 Python 中的所有文件

import and read all files from a folder Python

大家好,我是 python 的新手,我的代码有问题 我想从特定文件夹导入和读取所有 .BVH 文件,但程序只需要第一个folder.Here 是我的 code.I 使用 blender 进行可视化。

import bpy # This module gives access to blender data, classes, and functions
import os # This module provides a unified interface to a number of operating system functions.
import sys # This module provides a number of functions and variables that can be used to manipulate different parts of the Python runtime environment.

path = "C:\Users\PC\Desktop\Rotate Prototype\filtered"
dir = os.listdir("C:\Users\PC\Desktop\Rotate Prototype\filtered")

files = 0
for files in dir:
    if files.lower().endswith('.bvh'):
        try:

            bpy.ops.object.delete() # Deletes the cube

            bpy.ops.import_anim.bvh(filepath="C:\Users\PC\Desktop\Rotate Prototype\filtered\pick_001_3_fil_Take_001.bvh", axis_forward='-Z', axis_up='Y', filter_glob="*.bvh", target='ARMATURE', global_scale=1.0, frame_start=1, use_fps_scale=False, update_scene_fps=False, update_scene_duration=False, use_cyclic=False, rotate_mode='NATIVE') # We import a bvh file with the appropriate settings

            bpy.context.scene.render.fps = 72  # We configure the frame rate

            bpy.ops.export_anim.bvh(filepath="C:\Users\PC\Desktop\Rotate Prototype\trolled\haha.bvh", check_existing=True, filter_glob="*.bvh", global_scale=1.0, frame_start=1, frame_end=1515, rotate_mode='XYZ', root_transform_only=True) # We export the file with the appropriate settings

        except:
                print ("Couldn't open file")                
files++

您没有在 for 循环中使用实际文件。您只是每次都使用相同的硬编码路径。

也许你想要下面这样的东西?

我将 files 重命名为 file_path 以更好地表示该变量中的内容。然后我在 import_anim.bvh 的调用中使用了该值,然后我在 export_anim.bvh 的调用中再次使用了它。 (我在文件名末尾添加了 "_exported.bvh"。我不太确定您要做什么。)

for file_path in dir:
    if file_path.lower().endswith('.bvh'):
        try:
            bpy.ops.object.delete() # Deletes the cube

            # We import a bvh file with the appropriate settings
            bpy.ops.import_anim.bvh(filepath=file_path,
                axis_forward='-Z', axis_up='Y', filter_glob="*.bvh",
                target='ARMATURE', global_scale=1.0, frame_start=1,
                use_fps_scale=False, update_scene_fps=False,
                update_scene_duration=False, use_cyclic=False,
                rotate_mode='NATIVE')

            bpy.context.scene.render.fps = 72  # We configure the frame rate

            # We export the file with the appropriate settings
            bpy.ops.export_anim.bvh(
                filepath=file_path + '_exported.bvh',
                check_existing=True, filter_glob="*.bvh",
                global_scale=1.0, frame_start=1, frame_end=1515,
                rotate_mode='XYZ', root_transform_only=True)

        except:
            print ("Couldn't open file")                

您正在使用 files 在每次迭代中计算和保存当前文件路径。并且在迭代中您没有将当前文件路径输入 import_anim,您只是使用了硬编码文件路径。 此外,++ 不是有效的语法。

files = 0
for file_path in dir:
    if file_path.lower().endswith('.bvh'):
        try:
            bpy.ops.object.delete() # Deletes the cube
            bpy.ops.import_anim.bvh(filepath=file_path, axis_forward='-Z', axis_up='Y', filter_glob="*.bvh", target='ARMATURE', global_scale=1.0, frame_start=1, use_fps_scale=False, update_scene_fps=False, update_scene_duration=False, use_cyclic=False, rotate_mode='NATIVE') # We import a bvh file with the appropriate settings
            bpy.context.scene.render.fps = 72  # We configure the frame rate
            bpy.ops.export_anim.bvh(filepath=file_path, check_existing=True, filter_glob="*.bvh", global_scale=1.0, frame_start=1, frame_end=1515, rotate_mode='XYZ', root_transform_only=True) # We export the file with the appropriate settings
            files += 1
        except:
            print ("Couldn't open file: {}".format(file_path))