存档意外结束
嘿,我对编程还很陌生,并且在 Python 挑战赛中遇到了问题;我已经删除了确切的网址,希望避免严重剧透。
无论如何,我的问题是,我运行以下代码后尝试在 WinRAR 中打开我创建的文件,它告诉我该文件有“意外的存档结束”。当然,为了以防万一,我尝试重新运行我的代码几次,但仍然没有成功。
我还用浏览器从同一网址抓取了该文件,以确保文件本身没有损坏,并且打开它没有任何错误,所以我很困惑。我想我错过了这个过程的一些基本要素?
我提前感谢您的帮助!
import urllib
url = "http://www.pythonchallenge.com/pc/def/xxxxxxx.zip"
site = urllib.urlopen(url)
newfile = open(url.split('/')[-1],'w')
newfile.write(site.read())
site.close()
newfile.close()
Hey there, I'm pretty new to programming and I've got a problem with the Python Challenge; and I've removed the exact url in hopes avoiding any heavy spoilers.
Anyway, my problem is that I'm trying to open the file I've created, in WinRAR after I've ran the following code, and it tells me the file has an "unexpected of end of archive". Naturally I've tried to rerun my code a few times just in case, and still no luck.
I've also grabbed the file with my browser from the same url to make sure that the file itself isn't damaged, and opened it without any errors, so I'm pretty stumped. I guess I'm missing some basic element of the process?
I appreciate your help in advance!
import urllib
url = "http://www.pythonchallenge.com/pc/def/xxxxxxx.zip"
site = urllib.urlopen(url)
newfile = open(url.split('/')[-1],'w')
newfile.write(site.read())
site.close()
newfile.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜你使用的是 Windows 机器。 (主要是由于“WinRAR”)
'w'
打开文件进行写入,但处于“文本”模式。在文本模式下,某些操作系统(如 Windows)会将'\n'
转换为其他内容(在 Window 中为'\r\n'
)。为了避免这种翻译,请以二进制模式'b'
打开文件,并写入'w'
:'wb'
这些字母源自<代码>fopen。 查看
fopen
的手册页,据我所知它对标志的描述比 Python 文档 更好。 (但请注意,Python 在标志中添加了一些内容。)I'm guessing you're on a Windows machine. (Mostly due to "WinRAR")
The
'w'
opens the file for writing, but in "text" mode. In text mode, some OSs (like Windows) convert'\n'
to something else ('\r\n'
in Window's case.). To avoid this translation, open the file in binary mode'b'
, with writing'w'
:'wb'
These letters derive from
fopen
. See the manual page forfopen
, as I feel it has a better description of the flags than the Python docs. (Note however, that Python adds a few things to the flags.)