一、场景

工作需要,有时要将文件上传到 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的更多相关文章

  1. Notepad++中设置Windows、Unix、Mac三种行尾换行符格式间的转换

    (1)首先,要设置NotePad++能显示换行符,这个才干看到效果, 视图-->显示符号-->显示行尾符. {2}设置行尾符格式:编辑-->档案格式转换-->(可选Window ...

  2. Windows、Unix、Mac不同操作系统的换行问题-剖析回车符\r和换行符\n

    转载链接:http://blog.csdn.net/tskyfree/article/details/8121951 一.概念: 换行符‘\n’和回车符‘\r’ (1)换行符就是另起一行  --- ' ...

  3. 数据分析:基于Python的自定义文件格式转换系统

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  4. 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 ...

  5. 使用textutil进行文件格式转换

    Typorahtml, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMirror-scroll { ...

  6. Python3.6(windows系统)解决编码问题

    Python3.6(windows系统)解决编码问题 1,py文件代码: import urllib.request url = "http://www.douban.com/" ...

  7. WINDOWS和UNIX换行符的理解

    # WINDOWS和UNIX换行符的理解 **file1.txt**17.143.161.37   其他    美国54.163.255.40   其他    美国 弗吉尼亚州 亚马逊公司 **[ro ...

  8. Windows和Unix下的编码问题

    今天测试shell脚本时,执行报错: ./report.sh: /tmp/tmp.E8ekx6r5Qq/report.sh: /bin/bash^M: bad interpreter: No such ...

  9. C#下搭建文件格式转换服务器

    文件格式转换,相信很多涉及到office文档在线观看的都会需要,因为浏览器还不能完全支持直接打开office文档,所以很多情况下我们都需要将这些文档转换成flash能够播放的格式,但是另一个问题又来了 ...

随机推荐

  1. 【Redis】Linux下Redis安装与redis-desktop-manager使用(无法连接Redis服务器解决方法)

    新手小白开始学习nosql数据库Redis,首先从安装入手. 全文分两部分:Linux下安装redis,可视化工具redis desktop manager的简单使用. 一.安装 下载,解压缩和编译R ...

  2. 转:c# 安装包制作

    .net Windows服务程序和安装程序制作 最近项目中用到window服务程序,以前没接触过,比较陌生,花了两天的时间学习了下,写了个简单的服务,但在制作安装程序的时候,参照网上很多资料,却都制作 ...

  3. 使用UICollectionView

    使用UICollectionView 使用UICollectionView的流程: 1. 设定一个UICollectionViewFlowLayout 2. 使用这个设定的UICollectionVi ...

  4. [翻译] TransitionKit

    TransitionKit https://github.com/blakewatters/TransitionKit A simple, elegantly designed block based ...

  5. 获取系统屏幕尺寸参数的类WxHxD

    获取系统屏幕尺寸参数的类WxHxD 源码: // // WxHxD.h // PM2.5 // // Created by YouXianMing on 14/10/29. // Copyright ...

  6. Mac Item2 设置别名 永久生效

    使用 Item2 终端, 设置 别名的时候, 按照 网上的说法, 是 去 修改 用户目录下的   .bashrc   或者  .bash_profile 这两个文件都可以, 把 alias 写在 这两 ...

  7. HDFS下载数据机制的底层分析

    HDFS下载数据机制的底层分析 Hadoop中的RPC(Remote Procedure Call)框架 hadoop中结点间的通信采用的是RPC. RPC框架的实现机制图解: 从hdfs下载数据的源 ...

  8. 原生JS和jQuery分别使用jsonp来获取“当前天气信息”

    需掌握的技能点: jsonp.跨域相关等. 以下两种代码,均可直接运行. 1.使用原生JS: <!DOCTYPE html> <html lang="en"> ...

  9. Sequelize-nodejs-4-Model usage

    Model usage模型使用 Data retrieval / Finders Finder methods are intended to query data from the database ...

  10. sql server 2000 错误229 拒绝了对象sysobjects 的select 权限

    此问题是权限问题,我的解决办法是因为添加角色的时候勾选太多导致的 !!!!!!千万不要勾选db_denydatareader.