I have a Python script that can successfully write binary data to a file:
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:
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've already tested that I can write into the memory-mapped file (and see the results) with basic I/O in the following way:
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?
Thank you for any feedback!