使用python實(shí)現(xiàn)unix2dos和dos2unix命令的例子
由于工作電腦網(wǎng)絡(luò)限制無(wú)法安裝unix2dos和dos2unix命令轉(zhuǎn)換文件,自己實(shí)現(xiàn)一個(gè)
直接上代碼,保存為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實(shí)現(xiàn)unix2dos和dos2unix命令的例子就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python中不同類(lèi)之間調(diào)用方法的四種方式小結(jié)
類(lèi)是一種面向?qū)ο蟮木幊谭妒?它允許我們將數(shù)據(jù)和功能封裝在一個(gè)實(shí)體中,本文主要介紹了Python中不同類(lèi)之間調(diào)用方法的四種方式小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
利用python下載scihub成文獻(xiàn)為PDF操作
這篇文章主要介紹了利用python下載scihub成文獻(xiàn)為PDF操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
一篇文章帶你搞定Ubuntu中打開(kāi)Pycharm總是卡頓崩潰
這篇文章主要介紹了一篇文章帶你搞定Ubuntu中打開(kāi)Pycharm總是卡頓崩潰,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Python文件讀寫(xiě)常見(jiàn)用法總結(jié)
今天小編就為大家分享一篇關(guān)于Python文件讀寫(xiě)常見(jiàn)用法總結(jié),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02
python中pivot()函數(shù)基礎(chǔ)知識(shí)點(diǎn)
在本篇內(nèi)容里小編給大家分享的是一篇關(guān)于python中pivot()函數(shù)基礎(chǔ)知識(shí)點(diǎn)內(nèi)容,對(duì)此有興趣的朋友們可以參考學(xué)習(xí)下。2021-01-01
Python中requirements.txt簡(jiǎn)介(推薦)
Python項(xiàng)目中必須包含一個(gè)?requirements.txt?文件,用于記錄所有依賴(lài)包及其精確的版本號(hào),以便新環(huán)境部署,這篇文章主要介紹了Python中requirements.txt簡(jiǎn)介,需要的朋友可以參考下2022-11-11
python中學(xué)習(xí)K-Means和圖片壓縮
大家在python中會(huì)遇到關(guān)于K-Means和圖片壓縮的問(wèn)題,我先通過(guò)本次文章學(xué)習(xí)一下基本原理吧。2017-11-11
利用python實(shí)現(xiàn)平穩(wěn)時(shí)間序列的建模方式
這篇文章主要介紹了利用python實(shí)現(xiàn)平穩(wěn)時(shí)間序列的建模方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06

