ctypes CreateFile function not working properly
<p>creating new file with ctypes in python 3 doesnt seem to work properly, Here's how the code looks like: </p>

<pre><code>ctypes.windll.kernel32.CreateFileA (
  Filename, 0x10000000, 0x2,
  None, 1, 0x80, None
  )
</code></pre>

<p><em>the same code works perfectly in py 2.x</em><br>
I dont get any errors, and the file is created as well, only with no extension and name with only the first letter.
for e.g, If I have filename as:</p>

<pre><code>Filename = "File.txt"
</code></pre>

<p>It creates a file alright, but without "txt" extension and named as "F".<br>
I know there are other ways to create file which works, but i just need to know why its not working with ctypes?</p>

<p>You are calling the ANSI version of <code>CreateFile</code> while giving it Unicode string (The default string literal in python 3 are Unicode). To fix it :</p>

<hr>

<p>You can use <code>CreateFileA</code> and give it ANSI string.</p>

<pre><code>Filename = b'file.txt'

ctypes.windll.kernel32.CreateFileA (
  Filename, 0x10000000, 0x2,
  None, 1, 0x80, None
)
</code></pre>

<hr>

<p>You can use <code>CreateFileW</code>(The Unicode version of <code>CreateFile</code>)</p>

<pre><code>Filename = 'file.txt'

ctypes.windll.kernel32.CreateFileW (
  Filename, 0x10000000, 0x2,
  None, 1, 0x80, None
)
</code></pre>

<hr>

python python-3.x ctypes