使用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实现备份文件实例
Sep 16 Python
使用python实现正则匹配检索远端FTP目录下的文件
Mar 25 Python
使用Python发送各种形式的邮件的方法汇总
Nov 09 Python
python中如何使用正则表达式的集合字符示例
Oct 09 Python
python thrift搭建服务端和客户端测试程序
Jan 17 Python
python多进程实现文件下载传输功能
Jul 28 Python
Selenium定时刷新网页的实现代码
Oct 31 Python
使用Python将Mysql的查询数据导出到文件的方法
Feb 25 Python
解决Pycharm 导入其他文件夹源码的2种方法
Feb 12 Python
Python TKinter如何自动关闭主窗口
Feb 26 Python
python基础之匿名函数详解
Apr 21 Python
Pygame Time时间控制的具体使用详解
Nov 17 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
上海地方志办公室-上海电子仪表工业志
2021/03/04 无线电
完美解决dedecms中的[html][/html]和[code][/code]问题
2007/03/20 PHP
php生成zip文件类实例
2015/04/07 PHP
使用composer安装使用thinkphp6.0框架问题【视频教程】
2019/10/01 PHP
Javascript优化技巧(文件瘦身篇)
2008/01/28 Javascript
Array.prototype.slice.apply的使用方法
2010/03/17 Javascript
javascript的函数、创建对象、封装、属性和方法、继承
2011/03/10 Javascript
用JQUERY增删元素的代码
2012/02/14 Javascript
防止xss和sql注入:JS特殊字符过滤正则
2013/04/18 Javascript
express的中间件bodyParser详解
2014/12/04 Javascript
javascript中scrollTop详解
2015/04/13 Javascript
学习JavaScript设计模式(代理模式)
2015/12/03 Javascript
Javascript实现一个简单的输入关键字添加标签效果实例
2017/06/01 Javascript
JS实现搜索关键词的智能提示功能
2017/07/07 Javascript
Node.js中sequelize时区的配置方法
2017/12/10 Javascript
使用JavaScript实现node.js中的path.join方法
2018/08/12 Javascript
JS实现的贪吃蛇游戏案例详解
2019/05/01 Javascript
JS实现的检验身份证格式并输出出生日期,年龄,性别,出生地示例
2019/05/17 Javascript
微信小程序录音实现功能并上传(使用node解析接收)
2020/02/26 Javascript
浅谈JavaScript中你可能不知道URL构造函数的属性
2020/07/13 Javascript
python实现从网络下载文件并获得文件大小及类型的方法
2015/04/28 Python
使用py2exe在Windows下将Python程序转为exe文件
2016/03/04 Python
Python实现的生成格雷码功能示例
2018/01/24 Python
python selenium 获取标签的属性值、内容、状态方法
2018/06/22 Python
实现Python与STM32通信方式
2019/12/18 Python
pytorch中torch.max和Tensor.view函数用法详解
2020/01/03 Python
Python实现AI自动抠图实例解析
2020/03/05 Python
Django中的session用法详解
2020/03/09 Python
HTML5播放实现rtmp流直播
2020/06/16 HTML / CSS
大学校庆策划书
2014/01/31 职场文书
2014年变电站工作总结
2014/12/19 职场文书
司法廉洁教育心得体会
2016/01/20 职场文书
导游词之无锡梅园
2019/11/28 职场文书
Redis中有序集合的内部实现方式的详细介绍
2022/03/16 Redis
Android存储中最基本的文件存储方式
2022/04/30 Java/Android
Python OpenGL基本配置方式
2022/05/20 Python