【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能够播放的格式,但是另一个问题又来了 ...
随机推荐
- 读写appSettings配置节方法
<configuration> <appSettings> <add key="Socket_Path_MW_data" value=&q ...
- 树莓派raspberry Pi2 介绍
Compared to the Raspberry Pi 1 it has: A 900MHz quad-core ARM Cortex-A7 CPU 1GB RAM Like the (Pi 1) ...
- MDT配置数据库
连接数据库可以使用数据库账户和Windows集成身份验证两种方法:使用数据库账户:1.连接数据库时选择TCP/IP2.在数据库中添加相应规则后,配置数据库3.在CustomSettings.ini文件 ...
- 铁乐学Python_day09_函数
今天我们来学习函数. 产生的原由,若没有函数,会显得重复代码多,可读性差,也会造成重复造轮子的情景. 故产生了函数,用来封装好一个功能,它是以功能为导向的. 1.[函数的样式] 例如自己定义一个函数: ...
- eclipse 调试技巧收集
1 eclipse启动tomcat无法访问 原因:部署路径没有选择 tomcat安装路径 解决:双击,设置server location即可 http://blog.csdn.net/wqjsir/ ...
- openoffice centos7.4 安装
其他是配置好java环境后操作 1.下载软件 http://www.openoffice.org/download/other.html 2.安装 tar xf Apache_OpenOffice_4 ...
- 解决qpidd服务安装不上的问题
前几天对一个文件夹命名,忘记了qpidd的路径在这个文件夹的下面,导致后来qpidd用不了. 并且有打开计算机-管理-服务时,看到Advanced MessageQueuing Protocol 未启 ...
- win10不错的快捷键
A I S number 左右 上下 , Win键 Open Action center. Open Settings. Open Search. Open the app pinned to t ...
- 【原创】MySQL 生产环境备份还原
公司需要对生产环境的数据库进行备份,我接下了这份任务. 1. 首先谷歌了以下大家的备份方法 许多都使用到了Xtrabackup这个工具,超大型的公司可能有其他的的备份方法,这个工具对于中小型公司,甚至 ...
- singleton和prototype的区别
singleton作用域:当把一个Bean定义设置为singleton作用域是,Spring IoC容器中只会存在一个共享的Bean实例,并且所有对Bean的 请求,只要id与该Bean定义相匹配,则 ...