2

Is there a way to partition a USB drive multiple times and format each partition as a different type using Python 2.7.x?

Edit: Using Windows 7 as the operating system

  • Yes there is a way... :) – Willem Van Onsem Jul 28 '17 at 14:55
  • Yes, through system calls. What OS are you using? – zwer Jul 28 '17 at 14:55
  • I'm using windows 7, also added that information to the question. – user3215564 Jul 28 '17 at 14:57
  • There is no Python library for formatting disks I know of. (I'd be interested in your use case, by the way.) I would solve the issue by calling the corresponding command line utility of Windows (`fdisk` or similar?) using the `subprocess` Python module. – Alfe Jul 28 '17 at 15:06
  • I'm required to format the USB Stick and then partition it 3 times setting 2 of the partitions as a RAW and the 3rd as FAT32 the problem is there will be no access to admin permissions for any of the process so using system calls or any utility that requires those permissions will not work. – user3215564 Jul 28 '17 at 15:17

1 Answers1

0

as said in the comments you can use python subprocess with windows command line tools. formatting can be done using format

( Running windows shell commands with python <- you can also use diskpart with this )

for partitioning you can use the diskpart tool in combination with a batch file. the disadvantage is that you have to save the batch file on the computer

https://social.technet.microsoft.com/Forums/windows/en-US/c69d3bb5-328c-43e3-9239-837171e42b88/diskpart-batch-file?forum=w7itproinstall

with the following you can invoke a batch file from python

from subprocess import Popen
p = Popen("batch.bat", cwd=r"C:\Path\to\batchfolder")
stdout, stderr = p.communicate()

Run a .bat file using python code

ralf htp
  • 9,149
  • 4
  • 22
  • 34