gpt4 book ai didi

Python 写入映射文件 - 奇怪的行为

转载 作者:太空宇宙 更新时间:2023-11-04 01:45:37 24 4
gpt4 key购买 nike

我有一个 Python 脚本可以成功地将二进制数据写入文件:

iterable_array = [i + 32 for i in range(50)]
file_handle = open("a.bin", "wb")
bytes_to_write = bytearray(iterable_array)
file_handle.write(bytes_to_write)
file_handle.close()

但是,我收到以下错误:

Traceback (most recent call last):
File "python_100_chars.py", line 20, in <module>
file_handle = open("a.bin", "wb")
OSError: [Errno 22] Invalid argument: 'a.bin'

当我在执行以下创建文件映射并在按键后读取数据的程序(源代码最初来自 Microsoft 文档)时尝试编写:

HANDLE hFile = CreateFileA( "a.bin",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE | FILE_SHARE_READ,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
HANDLE hMapFile;
LPCTSTR pBuf;
hMapFile = CreateFileMapping(
hFile, // use paging file
NULL, // default security
PAGE_EXECUTE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
szName); // name of mapping object

if (hMapFile == NULL)
{
_tprintf(TEXT("Could not create file mapping object (%d).\n"),
GetLastError());
return 1;
}
pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());

CloseHandle(hMapFile);

return 1;
}


_getch();

printf("string inside file:%s",(char *)((void *)pBuf));
UnmapViewOfFile(pBuf);

CloseHandle(hMapFile);

我已经测试过我可以通过以下方式使用基本 I/O 写入内存映射文件(并查看结果):

HANDLE hFile =  CreateFileA(         "a.bin",
GENERIC_WRITE,
FILE_SHARE_WRITE | FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL ,
NULL);
char *p = "bonjour";
DWORD bw;
WriteFile( hFile,
p,
8,
&bw,
NULL);
  • python 脚本做了什么阻止它写入?

感谢您的任何反馈!

最佳答案

我没有 Windows,所以我不能完全测试这个行为,但我相信这是因为 Windows 不完全遵循 POSIX 语义,open(name, 'wb') 会,而不是截断 现有文件,用CREATE_ALWAYS 打开它这将与在另一个进程中映射的文件冲突。 ab 模式也可以工作,但是...作为 Python documentation says

'a' for appending (which on some Unix systems, means that all writes append to the end of the file regardless of the current seek position).

不幸的是,open 函数使用 C 语义作为标志,因此无法将所需模式指定为“打开文件以仅进行随机访问写入而不截断文件”,因此你能做的最好的事情是“打开它进行读写而不截断文件”,这将是 r+b


正如用户 Eryk Sun 所指出的,问题是 Windows 不支持 file truncation如果有任何现有的内存映射:

If CreateFileMapping is called to create a file mapping object for hFile, UnmapViewOfFile must be called first to unmap all views and call CloseHandle to close the file mapping object before you can call SetEndOfFile.

同样,没有比现有文件大的空间映射 - 如果映射比文件大小长,则文件被扩展...


在 POSIXly 正确的平台上,像这样的程序

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>


int main(int argc, char const *argv[])
{
struct stat s;
int fd = open("a.bin", O_RDWR);
fstat(fd, &s);
unsigned char *d = mmap(0, s.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
getchar();

for (int i = 0; i < s.st_size; i++) {
putchar(d[i]);
}

putchar('\n');
fflush(stdout);
getchar();
}

不会干扰上述 Python 程序的运行。但是,如果此 C 程序访问窗口内的文件,而该文件已被截断为 0,则会在 C 进程中引发 SIGBUS 信号。

关于Python 写入映射文件 - 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59126257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com