【python3】文件格式转换windows转为unix
一、场景
工作需要,有时要将文件上传到 linux 的服务器,希望将文件的格式改为 Unix(LF) 、 utf-8, 可以通过py脚本来批量处理。
二、代码
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2019/12/2 11:22
# @Author : way
# @Site :
# @Describe: 检查 脚本文件 转换格式为 LF , utf-8 import sys
import os
import chardet def turn(file):
with open(file, 'rb') as f:
data = f.read()
encoding = chardet.detect(data)['encoding']
data_str = data.decode(encoding)
tp = 'LF'
if '\r\n' in data_str:
tp = 'CRLF'
data_str = data_str.replace('\r\n', '\n')
if encoding not in ['utf-8', 'ascii'] or tp == 'CRLF':
with open(file, 'w', newline='\n', encoding='utf-8') as f:
f.write(data_str)
print(f"{file}: ({tp},{encoding}) trun to (LF,utf-8) success!") if __name__ == "__main__":
if sys.argv.__len__() != 2:
print(f"param: python3 etl_file_check.py /home/getway/script/hql")
else:
dr = sys.argv[1]
for path in os.listdir(dr):
file = os.path.join(dr, path)
if os.path.isfile(file):
turn(file)
【python3】文件格式转换windows转为unix的更多相关文章
- Notepad++中设置Windows、Unix、Mac三种行尾换行符格式间的转换
(1)首先,要设置NotePad++能显示换行符,这个才干看到效果, 视图-->显示符号-->显示行尾符. {2}设置行尾符格式:编辑-->档案格式转换-->(可选Window ...
- Windows、Unix、Mac不同操作系统的换行问题-剖析回车符\r和换行符\n
转载链接:http://blog.csdn.net/tskyfree/article/details/8121951 一.概念: 换行符‘\n’和回车符‘\r’ (1)换行符就是另起一行 --- ' ...
- 数据分析:基于Python的自定义文件格式转换系统
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- TensorFlow 1.2.0新版本完美支持Python3.6,windows在cmd中输入pip install tensorflow就能下载应用最新tensorflow
TensorFlow 1.2.0新版本完美支持Python3.6,windows在cmd中输入pip install tensorflow就能下载应用最新tensorflow 只需在cmd中输入pip ...
- 使用textutil进行文件格式转换
Typorahtml, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMirror-scroll { ...
- Python3.6(windows系统)解决编码问题
Python3.6(windows系统)解决编码问题 1,py文件代码: import urllib.request url = "http://www.douban.com/" ...
- WINDOWS和UNIX换行符的理解
# WINDOWS和UNIX换行符的理解 **file1.txt**17.143.161.37 其他 美国54.163.255.40 其他 美国 弗吉尼亚州 亚马逊公司 **[ro ...
- Windows和Unix下的编码问题
今天测试shell脚本时,执行报错: ./report.sh: /tmp/tmp.E8ekx6r5Qq/report.sh: /bin/bash^M: bad interpreter: No such ...
- C#下搭建文件格式转换服务器
文件格式转换,相信很多涉及到office文档在线观看的都会需要,因为浏览器还不能完全支持直接打开office文档,所以很多情况下我们都需要将这些文档转换成flash能够播放的格式,但是另一个问题又来了 ...
随机推荐
- Semaphore-信号灯机制
我们创建一个可扩展大小的线程池,并且需要在线程池内同时让有限数目的线程并发运行时,就需要用到Semaphore(信号灯机制),Semaphore 通常用于限制可以访问某些资源(物理或逻辑的)的线程数目 ...
- Linux内存管理(text、rodata、data、bss、stack&heap)
一.各内存区段的介绍 系统内的程序分为程序段和数据段,具体又可细分为一下几个部分: (1)text段-代码段 text段存放程序代码,运行前就已经确定(编译时确定),通常为只读,可以直接在ROM或Fl ...
- UITextField使用的一些细节
UITextField使用的一些细节 这篇博文是我自己使用UITextField的一些总结,并没有太多营养,并会持续更新. 2014.9.15 ---------------------------- ...
- 上拉加载下拉刷新控件WaterRefreshLoadMoreView
上拉加载下拉刷新控件WaterRefreshLoadMoreView 效果: 源码: // // SRSlimeView // @author SR // Modified by JunHan on ...
- [翻译] DraggableYoutubeFloatingVideo
DraggableYoutubeFloatingVideo DraggableYoutubeFloatingVideo allows you to play videos on a floating ...
- git 代码上传至远程仓库&从远程库克隆到本地
1.下载安装Git.下载:https://git-scm.com/downloads 安装:可参考文章http://blog.csdn.net/zzfenglin/article/details/5 ...
- Redis学习---Redis操作之List
List操作,redis中的List在在内存中按照一个name对应一个List来存储 lpush(name,values) --> 实际上是左添加 # 在name对应的list中添加元素,每个新 ...
- Linux 系统DNS解析原理
DNS:域名的解析,也称A记录,CDN服务器 配置文件位置: vi /etc/resolv.conf 解析原理 DNS就像一个倒挂的树,定点是点. www.baidu.com ==&g ...
- 基于php-fpm的配置详解
php5.3自带php-fpm/usr/local/php/etc/php-fpm.confpid = run/php-fpm.pidpid设置,默认在安装目录中的var/run/php-fpm.pi ...
- 所有文章的测试Demo
Mqtt C++ Client 测试Demo https://pan.baidu.com/s/1Ue00GYv2SUd8aTquhvOW1w