Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. import os
  2.  
  3. # 转换文件格式和编码方式
  4. def to_lf(path, isLF, encoding = 'utf-8'):
  5. """
  6. :param path: 文件路径
  7. :param isLF: True 转为Unix(LF) False 转为Windows(CRLF)
  8. :param encoding: 编码方式,默认utf-8
  9. :return:
  10. """
  11. newline = '\n' if isLF else '\r\n'
  12. tp = 'Unix(LF)' if isLF else 'Windows(CRLF)'
  13. with open(path, newline=None, encoding=encoding) as infile:
  14. str = infile.readlines()
  15. with open(path, 'w', newline=newline, encoding=encoding) as outfile:
  16. outfile.writelines(str)
  17. print("文件转换成功,格式:{0} ;编码:{1} ;路径:{2}".format(tp, encoding, path))
  18.  
  19. if __name__ == "__main__":
  20. rootdir = r'C:\Users\Administrator\Desktop\testl'
  21. isLF = True # True 转为Unix(LF) False 转为Windows(CRLF)
  22. path_list = os.listdir(rootdir)
  23. #path_list.sort(key=lambda x:int(x[:-4])) #对读取的路径进行排序
  24. for filename in path_list:
  25. path = os.path.join(rootdir,filename)
  26. to_lf(path, isLF)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement