开发者

Using python to write text files with DOS line endings on linux

I want to write text files with DOS/Windows line endings '\r\n' using python running on Linux. It seems to me that there must be a better way than manually putting a '\r\n' 开发者_开发百科at the end of every line or using a line ending conversion utility. Ideally I would like to be able to do something like assign to os.linesep the separator that I want to use when writing the file. Or specify the line separator when I open the file.


For Python 2.6 and later, the open function in the io module has an optional newline parameter that lets you specify which newlines you want to use.

For example:

import io
with io.open('tmpfile', 'w', newline='\r\n') as f:
    f.write(u'foo\nbar\nbaz\n')

will create a file that contains this:

foo\r\n
bar\r\n
baz\r\n


you can look at this PEP for some reference.

Update:

@OP, you can try creating something like this

import sys
plat={"win32":"\r\n", 'linux':"\n" } # add macos as well
platform=sys.platform
...
o.write( line + plat[platform] )


Just write a file-like that wraps another file-like and which converts \n to \r\n on write.

For example:

class ForcedCrLfFile(file):
    def write(self, s):
        super(ForcedCrLfFile, self).write(s.replace(r'\n', '\r\n'))


Here's a python script I wrote. It recurses from a given directory, replacing all \n line endings with \r\n endings. Use it like this:

unix2windows /path/to/some/directory

It ignores files in folders beginning with a '.'. It also ignores files that it thinks are binary files, using the approach given by J.F. Sebastian in this answer. You can filter further by using the optional regex positional argument:

unix2windows /path/to/some/directory .py$

Here's the script in full. For the avoidance of doubt, my parts are licensed under the MIT licence.

#!/usr/bin/python
import sys
import os
import re
from os.path import join

textchars = bytearray({7,8,9,10,12,13,27} | set(range(0x20, 0x100)) - {0x7f})
def is_binary_string(bytes):
    return bool(bytes.translate(None, textchars))

def is_binary_file(path):    
    with open(path, 'rb') as f:
        return is_binary_string(f.read(1024))

def convert_file(path):
    if not is_binary_file(path):
        with open(path, 'r') as f:
            text = f.read()
        print path
    with open(path, 'wb') as f:
        f.write(text.replace('\r', '').replace('\n', '\r\n'))

def convert_dir(root_path, pattern):
    for root, dirs, files in os.walk(root_path):
        for filename in files:
            if pattern.search(filename):
                path = join(root, filename)
                convert_file(path)

        # Don't walk hidden dirs
        for dir in list(dirs):
            if dir[0] == '.':
                dirs.remove(dir)

args = sys.argv
if len(args) <= 1 or len(args) > 3:
    print "This tool recursively converts files from Unix line endings to"
    print "Windows line endings"
    print ""
    print "USAGE: unix2windows.py PATH [REGEX]"
    print "Path:             The directory to begin recursively searching from"
    print "Regex (optional): Only files matching this regex will be modified"
    print ""    
else:
    root_path = sys.argv[1]
    if len(args) == 3:
        pattern = sys.argv[2]
    else:
        pattern = r"."
    convert_dir(root_path, re.compile(pattern))


You could write a function that converts text then writes it. For example:

def DOSwrite(f, text):
    t2 = text.replace('\n', '\r\n')
    f.write(t2)
#example
f = open('/path/to/file')
DOSwrite(f, "line 1\nline 2")
f.close()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜