相关疑难解决方法(0)

如何强制Python的file.write()在Windows中使用与Linux中相同的换行符格式("\ r \n"与"\n")?

我有简单的代码:

f = open('out.txt','w')
f.write('line1\n')
f.write('line2')
f.close()
Run Code Online (Sandbox Code Playgroud)

代码在Windows上运行并给出文件大小12 bytes,而linux给出11 bytes 的原因是新行

在Linux中它是\n胜利的\r\n

但在我的代码中,我将新行指定为\n.问题是如何让python像\n往常一样保持新行,而不是检查操作系统.

python newline

59
推荐指数
3
解决办法
12万
查看次数

如何使用\ r在同一行上打印?

我做了一个下载器:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from __future__ import print_function, division, absolute_import, unicode_literals
import os
import argparse
try:
    from urllib2 import urlopen
except ImportError:
    from urllib.request import urlopen

# {{ Argument parser
parser = argparse.ArgumentParser(
    prog='downloader',
    description='a featureful downloader'
)

parser.add_argument(
    '-o',
    dest='outputfile'
)

parser.add_argument(
    '-r',
    dest='remotefile'
)

downloader = parser.parse_args()
# }}

# {{ default local filename
if downloader.outputfile:
    default_output_file = downloader.outputfile
else:
    default_output_file = os.path.split(downloader.remotefile)[-1]
# }}

u = urlopen(downloader.remotefile)
meta = u.info()
file_size  = int(dict(meta.items())['Content-Length'])
print("Downloading: %s …
Run Code Online (Sandbox Code Playgroud)

python stdout carriage-return python-3.x

5
推荐指数
2
解决办法
3万
查看次数

标签 统计

python ×2

carriage-return ×1

newline ×1

python-3.x ×1

stdout ×1