Skip to main content
 首页 » 编程设计

python之dos2Unix 和 python 脚本的区别

2025年04月02日17傻小

我有文件要转换成 Unix 格式。 我在选择 python 转换方式时可能面临的差异/问题是什么:

import sys 
filename = sys.argv[1] 
text = open(filename, 'rb').read().replace('\r\n', '\n') 
open(filename, 'wb').write(text) 

而不是:在子进程中调用 dos2unix Unix 命令?

谢谢!

请您参考如下方法:

来自man dos2unix:

The Dos2unix package includes utilities "dos2unix" and "unix2dos" to convert plain text files in DOS or Mac format to Unix format and vice versa.

In DOS/Windows text files a line break, also known as newline, is a combination of two characters: a Carriage Return (CR) followed by a Line Feed (LF). In Unix text files a line break is a single character: the Line Feed (LF). In Mac text files, prior to Mac OS X, a line break was single Carriage Return (CR) character. Nowadays Mac OS uses Unix style (LF) line breaks.

Besides line breaks Dos2unix can also convert the encoding of files. A few DOS code pages can be converted to Unix Latin-1. And Windows Unicode (UTF-16) files can be converted to Unix Unicode (UTF-8) files.

...

-ascii Convert only line breaks. This is the default conversion mode.

dos2unix 因此可以做的不仅仅是转换换行符,但默认行为仅此而已。

如果您的文件编码错误,您也必须使用 dos2unix 来处理它。