如何将python里面的tab用空格替代
1个回答
展开全部
import os
lineList = []
postfixes = ['.py', '.java', '.c', '.cpp', '.h']
def deal_lines(file_name):
cmd = "dos2unix -ascii %s" %file_name
os.system(cmd)
with open(file_name, 'r') as f:
for line in f:
str = line.replace('\t', ' ').rstrip()
yield str + "\n"
def format_covert(file_path):
for path, dirs, files in os.walk(file_path):
for name in files:
full_path = os.path.join(path, name)
norm_path = os.path.normpath(os.path.abspath(full_path))
modifyFileFlag = any([norm_path.endswith(postfix) for postfix in postfixes])
if modifyFileFlag:
for line in deal_lines(norm_path):
lineList.append(line)
with open(norm_path, 'w+') as f:
for index in range(0, len(lineList)):
f.write(lineList[index])
del lineList[:]
if __name__ == '__main__':
file_path = raw_input('Please input a file path: ')
format_covert(file_path)
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询