使用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将xml和xsl转换为html的方法
Mar 10 Python
python实现中文分词FMM算法实例
Jul 10 Python
Python简单实现自动删除目录下空文件夹的方法
Aug 29 Python
python贪吃蛇游戏代码
Apr 18 Python
python3+pyqt5+itchat微信定时发送消息的方法
Feb 20 Python
Python一键安装全部依赖包的方法
Aug 12 Python
使用python批量修改文件名的方法(视频合并时)
Mar 24 Python
详解PyTorch手写数字识别(MNIST数据集)
Aug 16 Python
python 实现快速生成连续、随机字母列表
Nov 28 Python
Anaconda详细安装步骤图文教程
Nov 12 Python
python 邮件检测工具mmpi的使用
Jan 04 Python
Python的这些库,你知道多少?
Jun 09 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+AJAX实现无刷新注册(带用户名实时检测)
2006/12/02 PHP
PHP 使用header函数设置HTTP头的示例解析 表头
2013/06/17 PHP
PHP屏蔽过滤指定关键字的方法
2014/11/03 PHP
HTML中嵌入PHP的简单方法
2016/02/16 PHP
input链接页面、打开新网页等等的具体实现
2013/12/30 Javascript
前端轻量级MVC框架CanJS详解
2014/09/26 Javascript
原生js实现移动开发轮播图、相册滑动特效
2015/04/17 Javascript
jquery结合html实现中英文页面切换
2016/11/29 Javascript
Vue组件中slot的用法
2018/01/30 Javascript
详解使用jQuery.i18n.properties实现js国际化
2018/05/04 jQuery
详解React中setState回调函数
2018/06/14 Javascript
vue项目中使用多选框的实例代码
2020/07/22 Javascript
vue cli3.0打包上线静态资源找不到路径的解决操作
2020/08/03 Javascript
python网络编程学习笔记(10):webpy框架
2014/06/09 Python
Python Tkinter GUI编程入门介绍
2015/03/10 Python
Python的pycurl包用法简介
2015/11/13 Python
Python多线程、异步+多进程爬虫实现代码
2016/02/17 Python
解决Scrapy安装错误:Microsoft Visual C++ 14.0 is required...
2017/10/01 Python
在python中使用requests 模拟浏览器发送请求数据的方法
2018/12/26 Python
python实现AES加密解密
2019/03/28 Python
浅析Python3中的对象垃圾收集机制
2019/06/06 Python
window环境pip切换国内源(pip安装异常缓慢的问题)
2019/12/31 Python
Pytest框架之fixture的详细使用教程
2020/04/07 Python
jupyter修改文件名方式(TensorFlow)
2020/04/21 Python
python 深度学习中的4种激活函数
2020/09/18 Python
德国最大的网上足球商店:11teamsports
2019/09/11 全球购物
如何高效率的查找一个月以内的数据
2012/04/15 面试题
门诊挂号室室长岗位职责
2013/11/27 职场文书
消防工作实施方案
2014/06/09 职场文书
学习张林森心得体会
2014/09/10 职场文书
2015人事行政工作总结范文
2015/05/21 职场文书
企业培训简报范文
2015/07/20 职场文书
2015年入党积极分子培养考察意见
2015/08/12 职场文书
教您:房贷工资收入证明应该怎么写?
2019/08/19 职场文书
Java基础之线程锁相关知识总结
2021/06/30 Java/Android
25张裸眼3D图片,带你重温童年的记忆,感受3D的魅力
2022/02/06 杂记