python创建稀疏文件

import win32file
import winioctlcon

hFile = win32file.CreateFile('E:/1',
                             win32file.GENERIC_READ | win32file.GENERIC_WRITE,
                             win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE,
                             None,
                             win32file.CREATE_ALWAYS,
                             0,
                             None)
win32file.DeviceIoControl(hFile, winioctlcon.FSCTL_SET_SPARSE, None, 0)
win32file.SetFilePointer(hFile, 100*1024*1024, win32file.FILE_BEGIN)
win32file.SetEndOfFile(hFile)
win32file.CloseHandle(hFile)

先安装pywin32库,下载地址:

https://github.com/mhammond/pywin32/releases

接口文件: git\pywin32\win32\src\win32file.i

以DeviceIoControl为例, 函数参数参见代码中 keywords 和 @pyparm

// @pyswig str/buffer|DeviceIoControl|Sends a control code to a device or file system driver
// @comm Accepts keyword args
// @rdesc If a preallocated output buffer is passed in, the returned object
//	may be the original buffer, or a view of the buffer with only the actual
//	size of the retrieved data.
//	<nl>If OutBuffer is a buffer size and the operation is synchronous (ie no
//	Overlapped is passed in), returns a plain string containing the retrieved
//	data.  For an async operation, a new writeable buffer is returned.
PyObject *py_DeviceIoControl(PyObject *self, PyObject *args, PyObject *kwargs)
{
	OVERLAPPED *pOverlapped;
	PyObject *obhFile, *obInBuffer, *obOutBuffer, *ret=NULL;
	HANDLE hDevice;
	PyObject *obOverlapped = Py_None;

	DWORD dwIoControlCode;
	void *InBuffer=NULL, *OutBuffer=NULL;
	DWORD InBufferSize=0, OutBufferSize=0;
	BOOL bBuffer=FALSE;
	
	static char *keywords[]={"Device","IoControlCode","InBuffer","OutBuffer","Overlapped", NULL};
	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OkOO|O:DeviceIoControl", keywords,
		&obhFile,			// @pyparm <o PyHANDLE>|Device||Handle to a file, device, or volume
		&dwIoControlCode,	// @pyparm int|IoControlCode||IOControl Code to use, from winioctlcon
		&obInBuffer,		// @pyparm str/buffer|InBuffer||The input data for the operation, can be None for some operations.
		&obOutBuffer,		// @pyparm int/buffer|OutBuffer||Size of the buffer to allocate for output, or a writeable buffer
							//	as returned by <om win32file.AllocateReadBuffer>.
		&obOverlapped))		// @pyparm <o PyOVERLAPPED>|Overlapped|None|An overlapped object for async operations.  Device
							//	handle must have been opened with FILE_FLAG_OVERLAPPED.
		return NULL;

	if (!PyWinObject_AsHANDLE(obhFile, &hDevice))
		return NULL;
	if (!PyWinObject_AsReadBuffer(obInBuffer, &InBuffer, &InBufferSize, TRUE))
		return NULL;
	if (!PyWinObject_AsOVERLAPPED(obOverlapped, &pOverlapped, TRUE))
		return NULL;

	OutBufferSize=PyLong_AsLong(obOutBuffer);
	if (OutBufferSize!=(DWORD)-1 || !PyErr_Occurred()){
		// Return a writable buffer in asynch mode, otherwise a plain string
		//	for backward compatibility
		if (pOverlapped != NULL){
			ret=PyBuffer_New(OutBufferSize);
			if (ret==NULL)
				return NULL;
			if (!PyWinObject_AsWriteBuffer(ret, &OutBuffer, &OutBufferSize)){
				Py_DECREF(ret);
				return NULL;
				}
			}
		else{
			ret = PyString_FromStringAndSize(NULL, OutBufferSize);
			if (ret==NULL)
				return NULL;
			OutBuffer=PyString_AS_STRING(ret);
			}
		}
	else{
		PyErr_Clear();
		if (PyWinObject_AsWriteBuffer(obOutBuffer, &OutBuffer, &OutBufferSize, TRUE)){
			Py_INCREF(obOutBuffer);
			ret=obOutBuffer;
			bBuffer=TRUE;
			}
		else{
			PyErr_Clear();
			return PyErr_Format(PyExc_TypeError,
				"OutBuffer must be either a buffer size or writeable buffer object, not %s",
				obOutBuffer->ob_type->tp_name); 
			}
		}

	DWORD numRead;
	BOOL ok;
	Py_BEGIN_ALLOW_THREADS

	ok = DeviceIoControl(hDevice,
                         dwIoControlCode,
                         InBuffer,
                         InBufferSize,
                         OutBuffer, 
                         OutBufferSize,
                         &numRead,
                         pOverlapped);
	Py_END_ALLOW_THREADS

    if (!ok){
		DWORD err=GetLastError();
		// This error code is returned for a pending overlapped operation.
		if (err==ERROR_IO_PENDING)
			return ret;
		Py_DECREF(ret);
		return PyWin_SetAPIError("DeviceIoControl", err);
		}

	// If returned size less than requested buffer size, return only length of valid data
	if (numRead < OutBufferSize){
		if (bBuffer){
			// Create a view of existing buffer with actual output size
			// Memoryview object in py3k supports slicing
			#if (PY_VERSION_HEX >= 0x03000000)
				PyObject *resized=PySequence_GetSlice(ret, 0, numRead);
			#else
				PyObject *resized=PyBuffer_FromReadWriteObject(ret, 0, numRead);
			#endif
			Py_DECREF(ret);
			ret=resized;
			}
		else
			_PyString_Resize(&ret, numRead);
		}
	return ret;
}
PyCFunction pfnpy_DeviceIoControl=(PyCFunction)py_DeviceIoControl;
%}
%native(DeviceIoControl) pfnpy_DeviceIoControl;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值