简体   繁体   English

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

[英]import and read all files from a folder Python

Hi everyone i am beginner with python and i have a problem with my code i would like to import and read all the .BVH file from a specific folder but the program takes only the first one from the folder.Here is my code.I use blender for visualization. 嗨大家我是python的初学者,我的代码有问题我想导入和读取特定文件夹中的所有.BVH文件,但程序只从文件夹中获取第一个。这是我的代码。我使用搅拌器用于可视化。

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++

You're not using the actual file in the for loop. 您没有在for循环中使用实际文件。 You're just using the same hardcoded path each time. 您每次只使用相同的硬编码路径。

Maybe you want something like the below? 也许你想要下面这样的东西?

I renamed files to file_path to better represent what's in that variable. 我将files重命名为file_path以更好地表示该变量中的内容。 Then I used that value in the call to import_anim.bvh , and then I used it again in the call to export_anim.bvh . 然后我在对import_anim.bvh的调用中使用了该值,然后在对export_anim.bvh的调用中再次使用它。 (There I tacked on "_exported.bvh" to the end of the file name. I wasn't really sure what you were trying to do.) (在那里我将"_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")                

You are using files for both counting and holding the current file path in each iteration. 您正在使用files进行计数并在每次迭代中保存当前文件路径。 And in the iteration you don't input the current file path to import_anim , you just used a hard coded file path. 在迭代中,您不输入import_anim的当前文件路径,您只使用了硬编码文件路径。 Also, ++ is not a valid syntax. 此外, ++不是有效的语法。

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM