35676

'parted mklabel' raises an error through Python subprocess

Question:

I want to format a hard disk via python script using subprocess.Popen. Typing the following command inside a shell worls fine. Just pay attention with this command!

parted /dev/sdh mklabel gpt Warning: The existing disk label on /dev/sda will be destroyed and all data on this disk will be lost. Do you want to continue? Yes/No?

I agree by typing Yes and the disk is well formatted.

Rolling it inside Python subprocess, Popen exits with status code 1. I'm even not able to write 'Yes' into the stdin pipe.

Code as follows:

#test1 from subprocess import Popen, PIPE, STDOUT p = Popen('parted /dev/sdh mklabel gpt', shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT) print p.wait()

1

Or,

# test2 p = Popen('parted /dev/sdh mklabel gpt', shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT) p.stdin.write('Yes\n')

IOError: [Errno 32] Broken pipe

I'm not sure if Popen does not consider this warning as an error, and how could I change his behaviour if this is the case? Thanks for any suggestions.

Answer1:

By adding the -s option to the command as follows(ignore output), parted exits with success.

Popen('parted -s /dev/sdh mklabel gpt', shell=True)

Recommend

  • How to search data in all tables in database using select query in vb.net?
  • How to Pass data table from DataGridView to ReportViewer
  • Labelling files with spaces in ClearCase
  • what is the next step, i have the code but i dont know what to do next?
  • nuget package with multiple DLL's
  • Generate 'random' numbers between x and y with a difference of at least 100
  • get count of all positive values from a the last 150 rows at each row - pandas
  • Is it possible to lock the applying label option for a work_branch in clearcase?
  • ESB Toolkit and Enterprise Library collision with IIS Web.Config
  • Is it possible to lock the applying label option for a work_branch in clearcase?
  • Using Pandas To Find the Number Of Periods Since the Rolling High
  • Pandas Groupby TimeGrouper and apply
  • pandas MovingOLS doesn't support NA values?
  • Is there a way for a caller to get the output of a powershell function without subjecting it to (pos
  • Order string column is not working properly
  • handle AVL messages with iodine
  • Java JSON library that supports DOM-style access
  • In log4j2, how to configure renameEmptyFiles to be false for the RollingFile appender?
  • Counting how many times in a row the result of a sum is positive (or negative)
  • Did back key destroy an activity?
  • Recommended way to remove events on destroy with jQuery UI Widget Factory
  • How to change default stop edit behavior in jtable
  • Specify HTTPS for custom WCF Binding
  • Losing my session variables
  • ConnectivityManager.CONNECTIVITY_ACTION deprecated
  • Making Google Visualization - Annotation Chart to work in GWT
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • Android changing fragment order inside FragmentPagerAdapter
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Python pickle not one-to-one: different pickles give same object
  • How can I set a binding to a Combox in a UserControl?
  • Create DicomImage from scratch using Dcmtk
  • Groovy: Unexpected token “:”
  • Swift: Switch statement fallthrough behavior
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • embed rChart in Markdown
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app