随机写入Python中的文件访问 [英] random writing access to a file in Python

查看:88
本文介绍了随机写入Python中的文件访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个250 GB的文件(占用整个硬盘空间),并希望

在给定的appr偏移量下只更改此文件中的8个字节。 200

Gbyte(该文件中的所有其他数据应保持不变)。


如何在Python中执行此操作?


Claudio Grondi

解决方案

[Claudio Grondi]


我有一个250 Gbyte文件(占用整个硬盘空间)



然后Python存储在哪里;-)?


并且想要在给定的appr偏移量下仅更改此文件中的八个字节。 200

Gbyte(该文件中的所有其他数据应保持不变)。


如何在Python中执行此操作?



你在C中的表现一样,真的:


f = open(PATH_TO_FILE," r + b")

f.seek(约200 GB)

f.write(A_STRING_CONTAINING_YOUR_NEW_8_BYTES)

f.close()


这取决于您的操作系统和文件系统和平台C

库支持寻求非常大的偏移量。例如,使用NTFS的Windows

。试试吧。如果你的盒子拒绝合作,Python应该抱怨(在seek()尝试期间提出异常

)。尽可能使用最近发布的Python版本。


Tim Peters写道:


[Claudio Grondi]


>我有250 Gbyte文件(占用整个硬盘空间)




然后Python存储在哪里;-)?


>并希望在此文件中仅更改八个字节给予抵消
appr。 200
Gbyte(该文件中的所有其他数据应保持不变)。

如何在Python中执行此操作?




你用C做同样的方式,真的:


f = open(PATH_TO_FILE, r + b)

f.seek(约200 GB)

f.write(A_STRING_CONTAINING_YOUR_NEW_8_BYTES)

f.close( )


这取决于您的操作系统和文件系统和平台C

库支持寻求非常大的偏移量。例如,使用NTFS的Windows

。试试吧。如果你的盒子拒绝合作,Python应该抱怨(在seek()尝试期间提出异常

)。尽可能使用最近发布的Python版本作为




非常感谢您的快速回复。


我的问题的核心是......试图使用'' wb''或''w + b''...(愚蠢

我...)


Claudio Grondi


Dennis Lee Bieber写道:


星期五,2006年8月25日16:39:14 +0200,Claudio Grondi

< cl ************ @ freenet.dedeccimed comp.lang.python中的以下内容:


>>我的问题的核心是......试图使用''wb''或''w + b''...(愚蠢的我/她)...




哎呀......你有多少次从备份中恢复那个庞大的文件




我很聪明,可以先在一个非常小的文件上尝试它,想知道

是怎么回事。 Python文档甚至谷歌搜索'随机

文件访问Python''没有帮助,因为没有例子,没有

提示可用。


我在谷歌发现的Python中随机文件访问的唯一提示是

Python Cookbook的目录。来自O''Railly:
http:// www。 oreilly.com/catalog/pythoncook2/toc.html

以及关于随机阅读权限的提示。


我愚蠢到忘了' 'r +''(之前在很久以前用过了很多次,但是还没有在Python中使用过,但是还没有在Python中使用它)想太多了

Pythonic方式:


======================================== ========== =============

如果我想写,我不打开阅读(加或不加)

========================================= ========= =============


实际上我的档案是''仅''42 GByte,但我想构建

问题,无法建议使用中间文件。


介于两者之间我选择了一种全新的方法作为随机写入硬件/>
磁盘似乎实际上每次移动磁盘头在寻求的时候,所以

显然没有使用缓存来排序要写入

磁盘的部分,所以如果有很多这样的话就没有其他机会了尝试将
首先放在内存中,然后再将它们写入文件。这个

使得直观的直观编程变得有点复杂

因为要处理大文件需要以块的形式工作并且

浪费一些处理当他们没有填补空白时的结果。我想我

仍然不在正确的道路上,所以顺便说一下:


是否有现成的(免费的,最好的开源)工具能够对XXX GByte大文本文件(即

位置)中的行进行排序

(每行大约20个字节),充分利用可用内存来加速过程

尽可能多吗?


Claudio Grondi



I have a 250 Gbyte file (occupies the whole hard drive space) and want
to change only eight bytes in this file at a given offset of appr. 200
Gbyte (all other data in that file should remain unchanged).

How can I do that in Python?

Claudio Grondi

解决方案

[Claudio Grondi]

I have a 250 Gbyte file (occupies the whole hard drive space)

Then where is Python stored ;-)?

and want to change only eight bytes in this file at a given offset of appr. 200
Gbyte (all other data in that file should remain unchanged).

How can I do that in Python?

Same way you''d do it in C, really:

f = open(PATH_TO_FILE, "r+b")
f.seek(appr. 200 Gbyte)
f.write(A_STRING_CONTAINING_YOUR_NEW_8_BYTES)
f.close()

This depends on your operating system and file system and platform C
library supporting seeks to very large offsets. For example, Windows
using NTFS does. Try it. Python should complain (raise an exception
during the seek() attempt) if your box refuses to cooperate. Use as
recent a released version of Python as you can.


Tim Peters wrote:

[Claudio Grondi]

>I have a 250 Gbyte file (occupies the whole hard drive space)



Then where is Python stored ;-)?

>and want to change only eight bytes in this file at a given offset of
appr. 200
Gbyte (all other data in that file should remain unchanged).

How can I do that in Python?



Same way you''d do it in C, really:

f = open(PATH_TO_FILE, "r+b")
f.seek(appr. 200 Gbyte)
f.write(A_STRING_CONTAINING_YOUR_NEW_8_BYTES)
f.close()

This depends on your operating system and file system and platform C
library supporting seeks to very large offsets. For example, Windows
using NTFS does. Try it. Python should complain (raise an exception
during the seek() attempt) if your box refuses to cooperate. Use as
recent a released version of Python as you can.

Thank you much for the quick response.

The core of my problem was ... trying to use ''wb'' or ''w+b'' ... (stupid
me ...)

Claudio Grondi


Dennis Lee Bieber wrote:

On Fri, 25 Aug 2006 16:39:14 +0200, Claudio Grondi
<cl************@freenet.dedeclaimed the following in comp.lang.python:

>>The core of my problem was ... trying to use ''wb'' or ''w+b'' ... (stupid
me ...)



Ouch... How many times did you have to restore that massive file
from backup?

I was smart enough to try it first on a very small file wondering what
was happening. Python documentation and even Google search after ''random
file access in Python'' were not helpful as there was no example and no
hint available.

The only hint about random file access in Python I found with Google was
Table of Contents of "Python Cookbook" from O''Railly:
http://www.oreilly.com/catalog/pythoncook2/toc.html
and hints about random reading access.

I was stupid enough to forget about ''r+'' (used it many times before in
C/C++ a decade ago, but not yet in Python) thinking just too much the
Pythonic way:

================================================== =============
if I want to write, I don''t open for reading (plus or not plus)
================================================== =============

Actually my file was ''only'' 42 GByte, but I wanted to construct the
question making it impossible to suggest use of an intermediate file.

In between I have chosen a total new approach as random writing to hard
disk seems to actually move the disk head each time when seeking, so
apparently no cache is used sorting a bit the pieces to write to the
disk, so if there are many of them there is no other chance as to try to
put them together in memory first before writing them to the file. This
makes the straightforward intuitive programming a bit complicated
because to work on large files it is necessary to work in chunks and
waste some processing results when they don''t fill the gaps. I suppose I
am still not on the right path, so by the way:

Is there a ready to use (free, best Open Source) tool able to sort lines
(each line appr. 20 bytes long) of a XXX GByte large text file (i.e. in
place) taking full advantage of available memory to speed up the process
as much as possible?

Claudio Grondi


这篇关于随机写入Python中的文件访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆