使用python实现unix2dos和dos2unix命令的例子


Posted in Python onAugust 13, 2019

由于工作电脑网络限制无法安装unix2dos和dos2unix命令转换文件,自己实现一个

直接上代码,保存为python文件如unix2dos.py然后使用命令

unix2dos.py {unix2dos|dos2unix} {dirname|filename}
#! /usr/bin/env python
# coding=utf-8
 
import os
import sys
 
try:
  input = raw_input
except:
  pass
 
def usage():
  print('Usage:')
  print('\t %s' % ('unix2dos.py {unix2dos|dos2unix} {dirname|filename}'))
 
def err_exit(msg):
  if msg: print('%s' % msg)
  usage()
  sys.exit(0)
 
def getfiles(root):
  for dirpath, dirnames, filenames in os.walk(root):
    for filename in filenames:
      yield os.path.join(dirpath, filename)
 
def format_file(file, toformat='unix2dos'):
  print('Formatting %s:\t%s' % (toformat, file))
  if not os.path.isfile(file):
    print('ERROR: %s invalid normal file' % file)
    return
  if toformat == 'unix2dos':
    line_sep = '\r\n'
  else:
    line_sep = '\n'
  with open(file, 'r') as fd:
    tmpfile = open(file+toformat, 'w+b')
    for line in fd:
      line = line.replace('\r', '')
      line = line.replace('\n', '')
      tmpfile.write(line+line_sep)
    tmpfile.close()
    os.rename(file+toformat, file)
 
def uni_format_proc(filename, toformat):
  if not toformat or toformat not in ['unix2dos', 'dos2unix']:
    err_exit('ERROR: %s: Invalid format param' % (toformat))
  if not filename or not os.path.exists(filename):
    err_exit('ERROR: %s: No such file or directory' % (filename))
  if os.path.isfile(filename):
    format_file(filename, toformat)
    return
  if os.path.isdir(filename):
    for file in getfiles(filename):
      uni_format_proc(file, toformat)
 
if __name__ == '__main__':
  if len(sys.argv) != 3:
    err_exit('ERROR: Invalid arguments')
  uni_format_proc(filename=sys.argv[2], toformat=sys.argv[1])

以上这篇使用python实现unix2dos和dos2unix命令的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python装饰器使用实例:验证参数合法性
Jun 24 Python
Python 实现一行输入多个值的方法
Apr 21 Python
python高级特性和高阶函数及使用详解
Oct 17 Python
对python 读取线的shp文件实例详解
Dec 22 Python
python机器学习库scikit-learn:SVR的基本应用
Jun 26 Python
python实现切割url得到域名、协议、主机名等各个字段的例子
Jul 25 Python
Python魔法方法 容器部方法详解
Jan 02 Python
Python实现企业微信机器人每天定时发消息实例
Feb 25 Python
python GUI库图形界面开发之PyQt5美化窗体与控件(异形窗体)实例
Feb 25 Python
python入门:argparse浅析 nargs='+'作用
Jul 12 Python
Python结合百度语音识别实现实时翻译软件的实现
Jan 18 Python
总结Python连接CS2000的详细步骤
Jun 23 Python
Python编写带选项的命令行程序方法
Aug 13 #Python
使用python模拟命令行终端的示例
Aug 13 #Python
在macOS上搭建python环境的实现方法
Aug 13 #Python
解决Mac下使用python的坑
Aug 13 #Python
python 接口实现 供第三方调用的例子
Aug 13 #Python
python 爬取学信网登录页面的例子
Aug 13 #Python
利用anaconda作为python的依赖库管理方法
Aug 13 #Python
You might like
利用PHP创建动态图像
2006/10/09 PHP
UCenter Home二次开发指南
2009/05/28 PHP
php+jquery+html实现点击不刷新加载更多的实例代码
2016/08/12 PHP
详解PHP防止盗链防止迅雷下载的方法
2017/04/26 PHP
PHP实现统计所有字符在字符串中出现次数的方法
2017/10/17 PHP
PHP以json或xml格式返回请求数据的方法
2018/05/31 PHP
顶部缓冲下拉菜单导航特效的JS代码
2013/08/27 Javascript
JavaScript实现按Ctrl键打开新页面
2014/09/04 Javascript
JQuery实现的按钮倒计时效果
2015/12/23 Javascript
JavaScript几种数组去掉重复值的方法推荐
2016/04/12 Javascript
window.open打开窗口被拦截的快速解决方法
2016/08/04 Javascript
jQuery实现checkbox列表的全选、反选功能
2016/11/24 Javascript
深入理解Javascript箭头函数中的this
2017/02/13 Javascript
vue中的适配px2rem示例代码
2018/11/19 Javascript
JS数组扁平化(flat)方法总结详解
2019/06/24 Javascript
JavaScript实现简单的计算器
2020/01/16 Javascript
小程序websocket心跳库(websocket-heartbeat-miniprogram)
2020/02/23 Javascript
js+h5 canvas实现图片验证码
2020/10/11 Javascript
Vue 实现一个简单的鼠标拖拽滚动效果插件
2020/12/10 Vue.js
初学Python函数的笔记整理
2015/04/07 Python
Python三级目录展示的实现方法
2016/09/28 Python
Python探索之ModelForm代码详解
2017/10/26 Python
python如何实现int函数的方法示例
2018/02/19 Python
python中datetime模块中strftime/strptime函数的使用
2018/07/03 Python
Django中使用第三方登录的示例代码
2018/08/20 Python
pandas删除行删除列增加行增加列的实现
2019/07/06 Python
win10下安装Anaconda的教程(python环境+jupyter_notebook)
2019/10/23 Python
django自定义模板标签过程解析
2019/12/14 Python
纯CSS打造(无图像无js)的非常流行的讲话(语音)气泡效果
2012/12/28 HTML / CSS
草莓网化妆品日本站:Strawberrynet日本
2017/10/20 全球购物
将世界上最美丽的摄影作品转化为艺术作品:Photos.com
2017/11/28 全球购物
美国班级戒指、帽子和礼服、毕业产品、年鉴:Balfour
2018/11/01 全球购物
酒店优秀员工事迹材料
2014/06/02 职场文书
村级个人对照检查材料
2014/08/22 职场文书
党员四风问题对照检查材料思想汇报
2014/09/16 职场文书
2014年收银工作总结
2014/11/13 职场文书