从Python文件夹导入并读取所有文件

2024-06-02 13:45:15 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好,我是python的初学者,我的代码有问题,我想从一个特定的文件夹导入并读取所有的.BVH文件,但是程序只从文件夹。这里是我的代码。我使用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++

Tags: thetoimportfalsefilesthisframeusers
2条回答

你没有在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))

相关问题 更多 >