首页 > 开发技术 > indent格式化目录下的程序文件(含indent配置)

indent格式化目录下的程序文件(含indent配置)

2010年2月5日 发表评论 阅读评论

indent格式化目录下的所有文件(含indent配置)

一段python脚本,整理指定目录下的所有.cpp,.h,.c文件
python代码
import os
import sys
def walk_dir(dir,fileinfo,topdown=True):
  for root, dirs, files in os.walk(dir, topdown):
    for name in files:
      ext = os.path.splitext(os.path.join(name) )[1].lower()
      if ext == '.cpp' or ext == '.h' or ext == '.c':
        print( os.path.join(name))
        os.system( 'indent ' + os.path.join(root, name) )
        fileinfo.write( '  ' + os.path.join(root, name) + '\n')
 
dir = raw_input('please input the path:')
fileinfo = open('list.txt','w')
walk_dir(dir,fileinfo)
indent配置
-bad -bap -bbb -bbo -nbc -bl -bli0 -bls -c33 -cd33 -ncdb -ncdw -nce -cli0 -cp33 -cs -d0 -nbfda -nfc1 -nfca -hnl -ip5 -l80 -lp -prs -saf -sai -saw -nsc -nsob -nss -i4 -ts4 -nut -npcs -npsl

indent参数 值 含义
–blank-lines-after-declarations  bad  变量声明后加空行
–blank-lines-after-procedures  bap  函数结束后加空行
–blank-lines-before-block-comments  bbb  块注释前加空行
–break-before-boolean-operator  bbo  较长的行,在逻辑运算符前分行
–blank-lines-after-commas  nbc  变量声明中,逗号分隔的变量不分行
–braces-after-if-line  bl  ”if”和”{“分做两行
–brace-indent 0  bli0  ”{“不继续缩进
–braces-after-struct-decl-line  bls  定义结构,”struct”和”{“分行
–comment-indentationn  c33  语句后注释开始于行33
–declaration-comment-columnn  cd33  变量声明后注释开始于行33
–comment-delimiters-on-blank-lines  ncdb  不将单行注释变为块注释
–cuddle-do-while  ncdw  ”do — while”的”while”和其前面的”}”另起一行
–cuddle-else  nce  ”else”和其前面的”}”另起一行
–case-indentation 0  cli0  switch中的case语句所进0个空格
–else-endif-columnn  cp33  #else, #endif后面的注释开始于行33
–space-after-cast  cs  在类型转换后面加空格
–line-comments-indentation n  d0  单行注释(不从1列开始的),不向左缩进
–break-function-decl-args  nbfda  关闭:函数的参数一个一行
–declaration-indentationn  di2  变量声明,变量开始于2行,即不必对齐
–format-first-column-comments  nfc1  不格式化起于第一行的注释
–format-all-comments  nfca  不开启全部格式化注释的开关
–honour-newlines  hnl  Prefer to break long lines at the position of newlines in the input.
–indent-leveln  i4  设置缩进多少字符,如果为tab的整数倍,用tab来缩进,否则用空格填充。
–parameter-indentationn  ip5  旧风格的函数定义中参数说明缩进5个空格
–line-length 75  l75  非注释行最长75
–continue-at-parentheses  lp  续行从上一行出现的括号开始
–space-after-procedure-calls  pcs  函数和”(“之间插入一个空格
–space-after-parentheses  nprs  在”(”后”)”前不插入空格
–procnames-start-lines  psl  将函数名和返回类型放在两行定义
–space-after-for  saf  for后面有空格
–space-after-if  sai  if后面有空格
–space-after-while  saw  while后面有空格
–start-left-side-of-comments  nsc  不在生成的块注释中加*
–swallow-optional-blank-lines  nsob  不去掉可添加的空行
–space-special-semicolon  nss  一行的for或while语句,在”;”前不加空。
–tab-size  ts4  一个tab为4个空格(要能整除”-in”)
–use-tabs  ut  使用tab来缩进
Python遍历目录
Python的os模块,包含了普遍的操作系统功能,这里主要学习与路径相关的函数:
os.listdir(dirname):列出dirname下的目录和文件
os.getcwd():获得当前工作目录
os.curdir:返回当前目录(’.')
os.chdir(dirname):改变工作目录到dirname
os.path.isdir(name):判断name是不是一个目录,name不是目录就返回false
os.path.isfile(name):判断name是不是一个文件,不存在name也返回false
os.path.exists(name):判断是否存在文件或目录name
os.path.getsize(name):获得文件大小,如果name是目录返回0
os.path.abspath(name):获得绝对路径
os.path.normpath(path):规范path字符串形式
os.path.split(name):分割文件名与目录(事实上,如果你完全使用目录,它也会将最后一个目录作为文件名而分离,同时它不会判断文件或目录是否存在)
os.path.splitext():分离文件名与扩展名
os.path.join(path,name):连接目录与文件名或目录
os.path.basename(path):返回文件名
os.path.dirname(path):返回文件路径
1、os.path方法
通过传入需要遍历的目录,列出目录下的所有文件并统计文件数,os提供的path模块能对目录非常灵活的操作。
import os,sys
def listdir(dir,file):
file.write(dir + ‘\n’)
fielnum = 0
list = os.listdir(dir)  #列出目录下的所有文件和目录
for line in list:
filepath = os.path.join(dir,line)
if os.path.isdir(filepath):  #如果filepath是目录,则再列出该目录下的所有文件
myfile.write(‘   ‘ + line + ‘\\’+'\n’)
for li in os.listdir(filepath):
myfile.write(‘     ‘+li + ‘\n’)
fielnum = fielnum + 1
elif os.path:   #如果filepath是文件,直接列出文件名
myfile.write(‘   ‘+line + ‘\n’)
fielnum = fielnum + 1
myfile.write(‘all the file num is ‘+ str(fielnum))
dir = raw_input(‘please input the path:’)
myfile = open(‘list.txt’,'w’)
2、os.walk方法
os模块提供的walk方法很强大,能够把给定的目录下的所有目录和文件遍历出来。
方法:os.walk(path),遍历path,返回一个对象,他的每个部分都是一个三元组,(‘目录x’,[目录x下的目录list],目录x下面的文件)
import os
def walk_dir(dir,fileinfo,topdown=True):
for root, dirs, files in os.walk(dir, topdown):
for name in files:
print(os.path.join(name))
fileinfo.write(os.path.join(root,name) + ‘\n’)
for name in dirs:
print(os.path.join(name))
fileinfo.write(‘  ’ + os.path.join(root,name) + ‘\n’)
dir = raw_input(‘please input the path:’)
fileinfo = open(‘list.txt’,'w’)
walk_dir(dir,fileinfo)
topdown决定遍历的顺序,如果topdown为True,则先列举top下的目录,然后是目录的目录,依次类推,反之,则先递归列举出最深层的子目录,然后是其兄弟目录,然后子目录。

indent格式化目录下的所有文件(含indent配置)
一段python脚本,整理指定目录下的所有.cpp,.h,.c文件
python代码import osimport sys
def walk_dir(dir,fileinfo,topdown=True):    for root, dirs, files in os.walk(dir, topdown):        for name in files:            ext = os.path.splitext(os.path.join(name) )[1].lower()            if ext == ‘.cpp’ or ext == ‘.h’ or ext == ‘.c’:                print( os.path.join(name))                os.system( ‘indent ‘ + os.path.join(root, name) )                fileinfo.write( ‘  ’ + os.path.join(root, name) + ‘\n’)##        for name in dirs:#            print( os.path.join(name))#            fileinfo.write(‘  ’ + os.path.join(root,name) + ‘\n’)
dir = raw_input(‘please input the path:’)fileinfo = open(‘list.txt’,'w’)walk_dir(dir,fileinfo)
indent配置-bad -bap -bbb -bbo -nbc -bl -bli0 -bls -c33 -cd33 -ncdb -ncdw -nce -cli0 -cp33 -cs -d0 -nbfda -di2 -nfc1 -nfca -hnl -ip5 -l80 -lp -prs -psl -saf -sai -saw -nsc -nsob -nss -i4 -ts4 -nut -npcs
indent参数 值 含义 –blank-lines-after-declarations  bad  变量声明后加空行  –blank-lines-after-procedures  bap  函数结束后加空行  –blank-lines-before-block-comments  bbb  块注释前加空行  –break-before-boolean-operator  bbo  较长的行,在逻辑运算符前分行  –blank-lines-after-commas  nbc  变量声明中,逗号分隔的变量不分行  –braces-after-if-line  bl  ”if”和”{“分做两行  –brace-indent 0  bli0  ”{“不继续缩进  –braces-after-struct-decl-line  bls  定义结构,”struct”和”{“分行  –comment-indentationn  c33  语句后注释开始于行33  –declaration-comment-columnn  cd33  变量声明后注释开始于行33  –comment-delimiters-on-blank-lines  ncdb  不将单行注释变为块注释  –cuddle-do-while  ncdw  ”do — while”的”while”和其前面的”}”另起一行  –cuddle-else  nce  ”else”和其前面的”}”另起一行  –case-indentation 0  cli0  switch中的case语句所进0个空格  –else-endif-columnn  cp33  #else, #endif后面的注释开始于行33  –space-after-cast  cs  在类型转换后面加空格  –line-comments-indentation n  d0  单行注释(不从1列开始的),不向左缩进  –break-function-decl-args  nbfda  关闭:函数的参数一个一行  –declaration-indentationn  di2  变量声明,变量开始于2行,即不必对齐  –format-first-column-comments  nfc1  不格式化起于第一行的注释  –format-all-comments  nfca  不开启全部格式化注释的开关  –honour-newlines  hnl  Prefer to break long lines at the position of newlines in the input.  –indent-leveln  i4  设置缩进多少字符,如果为tab的整数倍,用tab来缩进,否则用空格填充。  –parameter-indentationn  ip5  旧风格的函数定义中参数说明缩进5个空格  –line-length 75  l75  非注释行最长75  –continue-at-parentheses  lp  续行从上一行出现的括号开始  –space-after-procedure-calls  pcs  函数和”(“之间插入一个空格  –space-after-parentheses  nprs  在”(”后”)”前不插入空格  –procnames-start-lines  psl  将函数名和返回类型放在两行定义  –space-after-for  saf  for后面有空格  –space-after-if  sai  if后面有空格  –space-after-while  saw  while后面有空格  –start-left-side-of-comments  nsc  不在生成的块注释中加*  –swallow-optional-blank-lines  nsob  不去掉可添加的空行  –space-special-semicolon  nss  一行的for或while语句,在”;”前不加空。  –tab-size  ts4  一个tab为4个空格(要能整除”-in”)  –use-tabs  ut  使用tab来缩进
Python遍历目录Python的os模块,包含了普遍的操作系统功能,这里主要学习与路径相关的函数:os.listdir(dirname):列出dirname下的目录和文件os.getcwd():获得当前工作目录os.curdir:返回当前目录(’.')os.chdir(dirname):改变工作目录到dirnameos.path.isdir(name):判断name是不是一个目录,name不是目录就返回falseos.path.isfile(name):判断name是不是一个文件,不存在name也返回falseos.path.exists(name):判断是否存在文件或目录nameos.path.getsize(name):获得文件大小,如果name是目录返回0os.path.abspath(name):获得绝对路径os.path.normpath(path):规范path字符串形式os.path.split(name):分割文件名与目录(事实上,如果你完全使用目录,它也会将最后一个目录作为文件名而分离,同时它不会判断文件或目录是否存在)os.path.splitext():分离文件名与扩展名os.path.join(path,name):连接目录与文件名或目录os.path.basename(path):返回文件名os.path.dirname(path):返回文件路径 1、os.path方法     通过传入需要遍历的目录,列出目录下的所有文件并统计文件数,os提供的path模块能对目录非常灵活的操作。import os,sysdef listdir(dir,file):    file.write(dir + ‘\n’)    fielnum = 0    list = os.listdir(dir)  #列出目录下的所有文件和目录    for line in list:        filepath = os.path.join(dir,line)        if os.path.isdir(filepath):  #如果filepath是目录,则再列出该目录下的所有文件            myfile.write(‘   ‘ + line + ‘\\’+'\n’)            for li in os.listdir(filepath):                myfile.write(‘     ‘+li + ‘\n’)                fielnum = fielnum + 1        elif os.path:   #如果filepath是文件,直接列出文件名            myfile.write(‘   ‘+line + ‘\n’)             fielnum = fielnum + 1    myfile.write(‘all the file num is ‘+ str(fielnum))dir = raw_input(‘please input the path:’)myfile = open(‘list.txt’,'w’)2、os.walk方法os模块提供的walk方法很强大,能够把给定的目录下的所有目录和文件遍历出来。方法:os.walk(path),遍历path,返回一个对象,他的每个部分都是一个三元组,(‘目录x’,[目录x下的目录list],目录x下面的文件)import osdef walk_dir(dir,fileinfo,topdown=True):    for root, dirs, files in os.walk(dir, topdown):        for name in files:            print(os.path.join(name))            fileinfo.write(os.path.join(root,name) + ‘\n’)        for name in dirs:            print(os.path.join(name))            fileinfo.write(‘  ’ + os.path.join(root,name) + ‘\n’)dir = raw_input(‘please input the path:’)fileinfo = open(‘list.txt’,'w’)walk_dir(dir,fileinfo)topdown决定遍历的顺序,如果topdown为True,则先列举top下的目录,然后是目录的目录,依次类推,反之,则先递归列举出最深层的子目录,然后是其兄弟目录,然后子目录。

分类: 开发技术 标签: , , 827 views
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.