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

 更新时间:2019年08月13日 16:47:52   作者:halazi100  
今天小编就为大家分享一篇使用python实现unix2dos和dos2unix命令的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

由于工作电脑网络限制无法安装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中不同类之间调用方法的四种方式小结

    类是一种面向对象的编程范式,它允许我们将数据和功能封装在一个实体中,本文主要介绍了Python中不同类之间调用方法的四种方式小结,具有一定的参考价值,感兴趣的可以了解一下
    2024-02-02
  • Python入门教程(五)Python变量的用法

    Python入门教程(五)Python变量的用法

    这篇文章主要介绍了Python入门教程(五)Python变量的用法,Python是一门非常强大好用的语言,也有着易上手的特性,本文为入门教程,需要的朋友可以参考下
    2023-04-04
  • 利用python下载scihub成文献为PDF操作

    利用python下载scihub成文献为PDF操作

    这篇文章主要介绍了利用python下载scihub成文献为PDF操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • 一篇文章带你搞定Ubuntu中打开Pycharm总是卡顿崩溃

    一篇文章带你搞定Ubuntu中打开Pycharm总是卡顿崩溃

    这篇文章主要介绍了一篇文章带你搞定Ubuntu中打开Pycharm总是卡顿崩溃,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Python文件读写常见用法总结

    Python文件读写常见用法总结

    今天小编就为大家分享一篇关于Python文件读写常见用法总结,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-02-02
  • Flask模拟实现CSRF攻击的方法

    Flask模拟实现CSRF攻击的方法

    这篇文章主要介绍了Flask模拟实现CSRF攻击的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • python中pivot()函数基础知识点

    python中pivot()函数基础知识点

    在本篇内容里小编给大家分享的是一篇关于python中pivot()函数基础知识点内容,对此有兴趣的朋友们可以参考学习下。
    2021-01-01
  • Python中requirements.txt简介(推荐)

    Python中requirements.txt简介(推荐)

    Python项目中必须包含一个 requirements.txt 文件,用于记录所有依赖包及其精确的版本号,以便新环境部署,这篇文章主要介绍了Python中requirements.txt简介,需要的朋友可以参考下
    2022-11-11
  • python中学习K-Means和图片压缩

    python中学习K-Means和图片压缩

    大家在python中会遇到关于K-Means和图片压缩的问题,我先通过本次文章学习一下基本原理吧。
    2017-11-11
  • 利用python实现平稳时间序列的建模方式

    利用python实现平稳时间序列的建模方式

    这篇文章主要介绍了利用python实现平稳时间序列的建模方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06

最新评论