a py to delete the trailling spaces and replace the "\t" with 4 spaces

#author: 54yuri

import os
import sys


def read_file( file_path ):
fd = open( file_path, "r" )
fd.seek( 0, os.SEEK_SET )
content = fd.read()
fd.close()

return content

def write_file( file_path, content ):
fd = open( file_path, "w" )

fd.seek( 0, os.SEEK_SET )
fd.write( content )
fd.flush()
fd.close()


def trim_trailling_spaces( old_content ):
new_content = ""
for line in old_content.splitlines():
line = line.rstrip(" ")
new_content += ( line + "\n" )

return new_content

def replace_tab_sign_with_spaces( old_content ):
new_content = ""
for line in old_content.splitlines():
line = line.replace( "\t", " " )
new_content += ( line + "\n" )
return new_content


def open_files( base_dir, file_ext_name ):

content = ""
for root, dirs, files in os.walk( base_dir ):
for file_name in files:
#print file_name
if ( os.path.splitext( file_name )[1] == file_ext_name ):
file_path = root + "/" + file_name
content = read_file( file_path )

print "%s need to be adjusted"%file_path
content = replace_tab_sign_with_spaces( content )
content = trim_trailling_spaces( content )

write_file( file_path, content )


def test1( ):
base_dir = "/tmp/abc"
file_ext_name = ".py"
open_files( base_dir, file_ext_name )


if __name__ == '__main__':
test1( )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值