通过os模块查找文件夹内的指定后缀文件,并批量导入。
import ansafrom ansa import baseimport osdef find_files(directory, extension):for root, dirs, files in os.walk(directory):for file in files:if file.endswith(extension):base.InputNastran((os.path.join(root, file)), elements_id="offset")# 指定目录和文件后缀directory = 'D:/temp'extension = '.bdf'find_files(directory, extension)
current_directory = os.getcwd()print(f"当前工作目录: {current_directory}")
os.chdir('/path/to/directory')print(f"目录已更改到: {os.getcwd()}")
files_and_directories = os.listdir('.')print(f"当前目录中的文件和目录: {files_and_directories}")
os.mkdir('new_directory')print("目录已创建")
os.makedirs('new_directory/sub_directory')print("递归目录已创建")
os.rmdir('new_directory')print("目录已删除")
os.remove('file.txt')print("文件已删除")
os.rename('old_name.txt', 'new_name.txt')print("文件已重命名")
stat_info = os.stat('example.txt')print(f"文件大小: {stat_info.st_size} 字节")print(f"最后修改时间: {stat_info.st_mtime}")
full_path = os.path.join('directory', 'subdirectory', 'file.txt')print(f"完整路径: {full_path}")
path, filename = os.path.split('/directory/subdirectory/file.txt')print(f"路径: {path}, 文件名: {filename}")
if os.path.exists('file.txt'):print("文件存在")else:print("文件不存在")
import osfor t in dir(os) :print(t)# DirEntry# EX_OK# F_OK# GenericAlias# Mapping# MutableMapping# O_APPEND# O_BINARY# O_CREAT# O_EXCL# O_NOINHERIT# O_RANDOM# O_RDONLY# O_RDWR# O_SEQUENTIAL# O_SHORT_LIVED# O_TEMPORARY# O_TEXT# O_TRUNC# O_WRONLY# P_DETACH# P_NOWAIT# P_NOWAITO# P_OVERLAY# P_WAIT# PathLike# R_OK# SEEK_CUR# SEEK_END# SEEK_SET# TMP_MAX# W_OK# X_OK# _AddedDllDirectory# _Environ# __all__# __builtins__# __doc__# __file__# __loader__# __name__# __package__# __spec__# _check_methods# _execvpe# _exists# _exit# _fspath# _get_exports_list# _walk# _wrap_close# abc# abort# access# add_dll_directory# altsep# chdir# chmod# close# closerange# cpu_count# curdir# defpath# device_encoding# devnull# dup# dup2# environ# error# execl# execle# execlp# execlpe# execv# execve# execvp# execvpe# extsep# fdopen# fsdecode# fsencode# fspath# fstat# fsync# ftruncate# get_exec_path# get_handle_inheritable# get_inheritable# get_terminal_size# getcwd# getcwdb# getenv# getlogin# getpid# getppid# isatty# kill# linesep# link# listdir# lseek# lstat# makedirs# mkdir# name# open# pardir# path# pathsep# pipe# popen# putenv# read# readlink# remove# removedirs# rename# renames# replace# rmdir# scandir# sep# set_handle_inheritable# set_inheritable# spawnl# spawnle# spawnv# spawnve# st# startfile# stat# stat_result# statvfs_result# strerror# supports_bytes_environ# supports_dir_fd# supports_effective_ids# supports_fd# supports_follow_symlinks# symlink# sys# system# terminal_size# times# times_result# truncate# umask# uname_result# unlink# unsetenv# urandom# utime# waitpid# waitstatus_to_exitcode# walk# write