Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'url:launchpad': 0.05; '-*-': 0.07; 'binary': 0.07; 'skip:` 10': 0.07; 'utf-8': 0.07; 'coding:': 0.09; 'parsed': 0.09; 'solution,': 0.09; 'used.': 0.09; 'translate': 0.10; 'python': 0.11; 'def': 0.12; 'windows': 0.15; "'w')": 0.16; 'from:addr:swing.be': 0.16; 'from:addr:vincent.vandevyvre': 0.16; 'from:name:vincent vande vyvre': 0.16; 'hex': 0.16; 'message-id:@swing.be': 0.16; 'mode,': 0.16; 'oqapy': 0.16; 'paqager': 0.16; 'received:mobistar.be': 0.16; 'url:oqapy': 0.16; 'url:paqager': 0.16; 'url:qarte': 0.16; 'v.v.': 0.16; 'win,': 0.16; '\xe9crit': 0.16; 'wrote:': 0.18; 'discussion': 0.18; 'module': 0.19; 'translated': 0.19; 'code,': 0.22; 'example': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'script': 0.25; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'skip:- 40': 0.29; 'mode': 0.30; "i'm": 0.30; 'code': 0.31; "d'aprano": 0.31; 'default,': 0.31; 'lies': 0.31; 'steven': 0.31; 'file': 0.32; 'run': 0.32; 'text': 0.33; 'open': 0.33; 'url:python': 0.33; 'problem': 0.35; 'subject:with': 0.35; 'editor': 0.35; 'but': 0.35; '+0200,': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'should': 0.36; 'so,': 0.37; 'skip:o 20': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'little': 0.38; 'to:addr:python.org': 0.39; 'is.': 0.60; 'manually': 0.60; 'tell': 0.60; 'url:3': 0.61; 'further': 0.61; 'here:': 0.62; 'different': 0.65; 'between': 0.67; 'fact,': 0.69; 'jul': 0.74; 'yourself': 0.78; 'reading,': 0.84; 'write()': 0.91; '2013': 0.98 Date: Wed, 24 Jul 2013 09:02:37 +0200 From: Vincent Vande Vyvre User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130623 Thunderbird/17.0.7 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Strange behaviour with os.linesep References: <368qu85msgfhuk2j2s13qj0bqn4rkcint9@4ax.com> <51ED3CEB.1070706@gmail.com> <51eea057$0$29971$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <51eea057$0$29971$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 65 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1374649363 news.xs4all.nl 15908 [2001:888:2000:d::a6]:57032 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51120 Le 23/07/2013 17:25, Steven D'Aprano a écrit : > On Tue, 23 Jul 2013 13:42:13 +0200, Vincent Vande Vyvre wrote: > >> On Windows a script where de endline are the system line sep, the files >> are open with a double line in Eric4, Notepad++ or Gedit but they are >> correctly displayed in the MS Bloc-Notes. > I suspect the problem lies with Eric4, Notepad++ and Gedit. Do you > perhaps have to manually tell them that the file uses Windows line > separators? > > I recommend opening the file in a hex editor and seeing for yourself what > line separators are used. > > >> Example with this code: >> ---------------------------------------------- >> # -*- coding: utf-8 -*- >> >> import os >> L_SEP = os.linesep >> >> def write(): >> strings = ['# -*- coding: utf-8 -*-\n', >> 'import os\n', >> 'import sys\n'] >> with open('writetest.py', 'w') as outf: >> for s in strings: >> outf.write(s.replace('\n', L_SEP)) >> >> write() >> ---------------------------------------------- >> >> The syntax `s.replace('\n', L_SEP)`is required for portability. > I don't think it is. Behaviour is a little different between Python 2 and > 3, but by default, Python uses "Universal Newlines". When you open a file > in text mode, arbitrary line separators should be automatically > translated to \n when reading, and \n will be automatically translated to > os.line_sep when writing. > > > http://docs.python.org/3/library/functions.html#open > http://docs.python.org/2/library/functions.html#open > > Some further discussion here: > > http://stackoverflow.com/questions/12193047/is-universal-newlines-mode- > supposed-to-be-default-behaviour-for-open-in-python > > > In fact, in my code, the original file is open in binary mode, the line separator is translate to \n and it is parsed by the module tokenise. I'm not a Windows user but my code must be run also on Win, this is the reason of the usage of os.linesep in writting. So, now I found the solution, just write the file in binary mode and now it is correctly open with all the editors. Thanks all -- Vincent V.V. Oqapy . Qarte . PaQager