python -- OS文件处理
获取当前文件夹下文件
# 导入os模块
import sys
import os
import jinja2
from time import ctime
import openpyxl
import pandas as pd
import numpy as np
import re
# path定义要获取的文件名称的目录(C盘除外)
path = os.getcwd()
print(path)
# os.listdir()方法获取文件夹名字,返回数组
file_name_list = os.listdir(path)
# 转为转为字符串
file_name = str(file_name_list)
print(file_name)
d:\myNotes\CNBLOGS\Python
['111', 'dest_file.txt', 'ipynb2md.sh', 'python -- 文件处理.ipynb', 'python -- 文件处理.md', 'python_jinja2_uvmgen.ipynb', 'templates', 'vscode_snippets.md', 'wesley', 'wesley_uvm_gen.py', '文件list.txt']
判断是否为文件夹
## 判断是否为文件夹
print(os.path.isdir(file_name_list[0]));
map0 = map(lambda x : os.path.isdir(x), [x for x in file_name_list])
##del list
isdir_list = list(map0)
print(isdir_list)
print(file_name_list)
True
[True, False, False, False, False, False, True, False, True, False, False]
['111', 'dest_file.txt', 'ipynb2md.sh', 'python -- 文件处理.ipynb', 'python -- 文件处理.md', 'python_jinja2_uvmgen.ipynb', 'templates', 'vscode_snippets.md', 'wesley', 'wesley_uvm_gen.py', '文件list.txt']
遍历所有文件夹: os.walk()
os.walk()可以遍历文件夹中的所有子文件夹及子文件, walk函数返回一个元组(dirpath, dirnames, filenames)
- dirpath是文件夹路径
- dirnames是文件夹名称
- filenames是文件名称
遍历并输出所有 文件
import os
for dirpath, dirnames, filenames in os.walk('.'):
for filename in filenames:
print(os.path.join(dirpath, filename))
.\dest_file.txt
.\ipynb2md.sh
.\python -- 文件处理.ipynb
.\python -- 文件处理.md
.\python_jinja2_uvmgen.ipynb
.\vscode_snippets.md
.\wesley_uvm_gen.py
.\文件list.txt
.\templates\dut.f
.\templates\dut.v
.\templates\filelist.f
.\templates\Makefile
.\templates\test_pkg.sv
.\templates\test_top.sv
.\templates\wave.tcl
.\templates\_agent.sv
.\templates\_agent_list.incl
.\templates\_base_seq.sv
.\templates\_base_test.sv
.\templates\_driver.sv
.\templates\_env.sv
.\templates\_env_list.incl
.\templates\_intf.sv
.\templates\_monitor.sv
.\templates\_ral.sv
.\templates\_reference.sv
.\templates\_reg_env.sv
.\templates\_scoreboard.sv
.\templates\_sequencer.sv
.\templates\_seq_item.sv
.\templates\_test_config.sv
.\templates\_test_list.incl
.\wesley\rtl\dut.f
.\wesley\rtl\dut.v
.\wesley\scripts\filelist.f
.\wesley\scripts\Makefile
.\wesley\scripts\wave.tcl
.\wesley\tb\test_pkg.sv
.\wesley\tb\test_top.sv
.\wesley\tb\wesley_intf.sv
.\wesley\tb\env\wesley_env.sv
.\wesley\tb\env\wesley_env_list.incl
.\wesley\tb\env\wesley_reference.sv
.\wesley\tb\env\wesley_scoreboard.sv
.\wesley\tb\env\agent\my0_agent.sv
.\wesley\tb\env\agent\my0_agent_list.incl
.\wesley\tb\env\agent\my0_driver.sv
.\wesley\tb\env\agent\my0_monitor.sv
.\wesley\tb\env\agent\my0_sequencer.sv
.\wesley\tb\env\agent\my0_seq_item.sv
.\wesley\tb\ral\wesley_ral.sv
.\wesley\tb\ral\wesley_reg_env.sv
.\wesley\tc\wesley_base_test.sv
.\wesley\tc\wesley_test_config.sv
.\wesley\tc\wesley_test_list.incl
.\wesley\tc\sequences\my0_base_seq.sv
.\wesley\tc\sequences\wesley_base_seq.sv
遍历并输出所有 文件夹
import os
for dirpath, dirnames, filenames in os.walk('.'):
for dirname in dirnames:
print(os.path.join(dirpath, dirname))
.\111
.\templates
.\wesley
.\wesley\rtl
.\wesley\scripts
.\wesley\tb
.\wesley\tc
.\wesley\work
.\wesley\tb\env
.\wesley\tb\ral
.\wesley\tb\env\agent
.\wesley\tc\sequences
对所有文件内容copy到一个文件内
walk_path = os.path.join('.', 'templates')
dest_file = open('dest_file.md', 'w')
for root, dirs, files in os.walk(walk_path):
##print(root, dirs, files, end='\n')
for f in files:
file0 = open(os.path.join('.', 'templates',f), 'r')
s0 = file0.read()
dest_file.write('\n' + '## ' + f + '\n')
dest_file.write('```Verilog\n')
dest_file.write(s0)
dest_file.write('\n```\n')
dest_file.close()
本文来自博客园,作者:Thisway2014,转载请注明原文链接:https://www.cnblogs.com/thisway2014/p/16524966.html