home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_win32_Demos_win32fileDemo.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  3.0 KB  |  78 lines

  1. # Test/Demo the native win32 file API for Python.
  2. import win32file, win32api, pywintypes, win32event
  3. import os
  4. import time
  5.  
  6. # todo: Add more tests/demos to make this truly useful!
  7. def OverlappedTest():
  8.     # Create a file in the %TEMP% directory.
  9.     testName = os.path.join( win32api.GetTempPath(), "win32filetest.dat" )
  10.     desiredAccess = win32file.GENERIC_WRITE
  11.     overlapped = pywintypes.OVERLAPPED()
  12.     evt = win32event.CreateEvent(None, 0, 0, None)
  13.     overlapped.hEvent = evt
  14.     # Create the file and write shit-loads of data to it.
  15.     h = win32file.CreateFile( testName, desiredAccess, 0, None, win32file.CREATE_ALWAYS, 0, 0)
  16.     chunk_data = "z" * 0x8000
  17.     num_loops = 512
  18.     expected_size = num_loops * len(chunk_data)
  19.     for i in range(num_loops):
  20.         win32file.WriteFile(h, chunk_data, overlapped)
  21.         win32event.WaitForSingleObject(overlapped.hEvent, win32event.INFINITE)
  22.         overlapped.Offset = overlapped.Offset + len(chunk_data)
  23.     h.Close()
  24.     # Now read the data back overlapped
  25.     overlapped = pywintypes.OVERLAPPED()
  26.     evt = win32event.CreateEvent(None, 0, 0, None)
  27.     overlapped.hEvent = evt
  28.     desiredAccess = win32file.GENERIC_READ
  29.     h = win32file.CreateFile( testName, desiredAccess, 0, None, win32file.OPEN_EXISTING, 0, 0)
  30.     buffer = win32file.AllocateReadBuffer(0xFFFF)
  31.     while 1:
  32.         try:
  33.             hr, data = win32file.ReadFile(h, buffer, overlapped)
  34.             win32event.WaitForSingleObject(overlapped.hEvent, win32event.INFINITE)
  35.             overlapped.Offset = overlapped.Offset + len(data)
  36.             
  37.             assert data is buffer, "Unexpected result from ReadFile - should be the same buffer we passed it"
  38.         except win32api.error:
  39.             break
  40.     h.Close()
  41. # A simple test using normal read/write operations.
  42. def Test():
  43.     # Create a file in the %TEMP% directory.
  44.     testName = os.path.join( win32api.GetTempPath(), "win32filetest.dat" )
  45.     desiredAccess = win32file.GENERIC_READ | win32file.GENERIC_WRITE
  46.     # Set a flag to delete the file automatically when it is closed.
  47.     fileFlags = win32file.FILE_FLAG_DELETE_ON_CLOSE
  48.     h = win32file.CreateFile( testName, desiredAccess, win32file.FILE_SHARE_READ, None, win32file.CREATE_ALWAYS, fileFlags, 0)
  49.     
  50.     # Write a known number of bytes to the file.
  51.     data = "z" * 1024
  52.     
  53.     win32file.WriteFile(h, data)
  54.     
  55.     if win32file.GetFileSize(h) != len(data):
  56.         print "WARNING: Written file does not have the same size as the length of the data in it!"
  57.         print "Reported size is", win32file.GetFileSize(h), "but expected to be", len(data)
  58.  
  59.     # Now truncate the file at 1/2 its existing size.
  60.     newSize = len(data)/2
  61.     win32file.SetFilePointer(h, newSize, win32file.FILE_BEGIN)
  62.     win32file.SetEndOfFile(h)
  63.     if win32file.GetFileSize(h) != newSize:
  64.         print "WARNING: Truncated file does not have the expected size!"
  65.         print "Reported size is", win32file.GetFileSize(h), "but expected to be", newSize
  66.     
  67.     h = None # Close the file by removing the last reference to the handle!
  68.     
  69.     if os.path.isfile(testName):
  70.         print "WARNING: After closing the file, it still exists!"
  71.         
  72.  
  73. if __name__=='__main__':
  74.     Test()
  75.     OverlappedTest()
  76.     print "Successfully performed some basic tests of win32file!"
  77.  
  78.