How to use python remove the '^M' when copy words from Windows to Linux
今天帮同事用Python写了一个小工具,实现了在linux下批量文件名和去掉windows 文件到linux过程中产生^M的脚本,代码如下:
1 !/opt/exptools/bin/python 2 import os,os.path,sys 3 import shutil,string 4 dir ='/home/alzhong/tools/zch/filedir' 5 6 7 for i in os.listdir(dir): 8 9 10 newfile = i.replace('.s.txt','.s') 11 12 13 oldfullfile = dir+"/"+i 14 newfullfile = dir+'/'+newfile 15 #corrform_cmd="winrar a '%s' %s" % (target, source) 16 try: 17 corrform_cmd="cat %s | tr -d \"\r\" > %s"%(oldfullfile, newfullfile) 18 os.system(corrform_cmd) 19 print "%s transform to %s successfully!"%(i, newfile) 20 except: 21 print "%s transform to %s failed!"%(i, newfile) 22 try: 23 rm_cmd="rm -rf %s"%(oldfullfile) 24 os.system(rm_cmd) 25 print "%s removed successfully!"%(i) 26 except: 27 print "%s removed failed!"%(i) 28 #shutil.move(oldfullfile,newfullfile)