I have a Python script that can successfully write binary data to a file:
(我有一个可以成功将二进制数据写入文件的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()
However, I get the following error:
(但是,出现以下错误:)
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'
when I try to write while executing the following program (source originally from Microsoft docs) that creates a file mapping and reads the data after a keypress:
(当我尝试在执行以下程序(最初来自Microsoft docs)时编写时,该程序创建文件映射并在按键后读取数据:)
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).
"),
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).
"),
GetLastError());
CloseHandle(hMapFile);
return 1;
}
_getch();
printf("string inside file:%s",(char *)((void *)pBuf));
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
I've already tested that I can write into the memory-mapped file (and see the results) with basic I/O in the following way:
(我已经测试过可以通过以下方式使用基本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);
- What is the python script doing that prevents it from writing?
(python脚本在做什么阻止它编写?)
Thank you for any feedback!
(感谢您的任何反馈!)
ask by thehorseisbrown translate from so