使用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当中的数据类型和变量
Apr 25 Python
python中__call__内置函数用法实例
Jun 04 Python
python Django模板的使用方法
Jan 14 Python
python利用拉链法实现字典方法示例
Mar 25 Python
Windows平台Python连接sqlite3数据库的方法分析
Jul 12 Python
python数字图像处理之高级滤波代码详解
Nov 23 Python
使用Python如何测试InnoDB与MyISAM的读写性能
Sep 18 Python
Python实现剪刀石头布小游戏(与电脑对战)
Dec 31 Python
django-xadmin根据当前登录用户动态设置表单字段默认值方式
Mar 13 Python
解决Django部署设置Debug=False时xadmin后台管理系统样式丢失
Apr 07 Python
keras中的卷积层&池化层的用法
May 22 Python
基于OpenCV的网络实时视频流传输的实现
Nov 15 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 set_time_limit(0) 设置程序执行时间的函数
2010/05/26 PHP
浅析php中常量,变量的作用域和生存周期
2013/08/10 PHP
php cookie使用方法学习笔记分享
2013/11/07 PHP
php header函数的常用http头设置
2015/06/25 PHP
实现WordPress主题侧边栏切换功能的PHP脚本详解
2015/12/14 PHP
php简单获取复选框值的方法
2016/05/11 PHP
ThinkPHP 3.2.2实现事务操作的方法
2017/05/05 PHP
PHP的imageTtfText()函数深入详解
2021/03/03 PHP
prototype 源码中文说明之 prototype.js
2006/09/22 Javascript
用javascript做拖动布局的思路
2008/05/31 Javascript
Javascript 构造函数 实例分析
2008/11/26 Javascript
javascript检查浏览器是否支持flash的实现代码
2014/08/14 Javascript
使用jQuery和Bootstrap实现多层、自适应模态窗口
2014/12/22 Javascript
JavaScript中的数组操作介绍
2014/12/30 Javascript
javascript中if和switch,==和===详解
2015/07/30 Javascript
JS实现的表格行上下移动操作示例
2016/08/03 Javascript
深入理解JS中的Function.prototype.bind()方法
2016/10/11 Javascript
Angularjs自定义指令实现三级联动 选择地理位置
2017/02/13 Javascript
利用JavaScript如何查询某个值是否数组内
2017/07/30 Javascript
详谈表单重复提交的三种情况及解决方法
2017/08/16 Javascript
微信小程序 如何引入外部字体库iconfont的图标
2018/01/31 Javascript
基于vue-resource jsonp跨域问题的解决方法
2018/02/03 Javascript
jQuery实现文件编码成base64并通过AJAX上传的方法
2018/04/12 jQuery
在vue中实现点击选择框阻止弹出层消失的方法
2018/09/15 Javascript
vue学习笔记之过滤器的基本使用方法实例分析
2020/02/01 Javascript
python实现巡检系统(solaris)示例
2014/04/02 Python
wxPython实现整点报时
2019/11/18 Python
导致python中import错误的原因是什么
2020/07/01 Python
H&M美国官网:欧洲最大的服饰零售商
2016/09/07 全球购物
CheapTickets泰国:廉价航班,查看促销价格并预订机票
2019/12/28 全球购物
电大自我鉴定
2013/10/27 职场文书
2014年团总支工作总结
2014/11/21 职场文书
工伤事故赔偿协议书
2015/08/06 职场文书
拒绝盗图!教你怎么用python给图片加水印
2021/06/04 Python
Redis 中使用 list,streams,pub/sub 几种方式实现消息队列的问题
2022/03/16 Redis
Java中的随机数Random
2022/03/17 Java/Android