我有简单的代码:
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往常一样保持新行,而不是检查操作系统.
我做了一个下载器:
#!/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)