I am trying to move some dos command from my batch file in python but get this error. The file name, directory name, or volume label syntax is incorrect for the following statement.
subprocess.Popen('rd /s /q .\ProcessControlSimulator\bin', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
If I just copy this dos command to the window console, it works. The os.getcwd () command gave me the expected working directory.
My questions: 1. Why? 2. How to avoid this? Do I need to get the current working directory and build an abstract path for this command? how to do it?
thank
\ ( ) escape- , . double \ (, \\) :
\
\\
subprocess.Popen('rd /s /q .\\ProcessControlSimulator\\bin', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
, . Python, , . , , ? , shutil. :
import shutil import os path = os.path.join("c:\\","ProcessControlSimulator","bin") #example only try: shutil.rmtree(path) except Exception,e: print e else: print "removed"
, os.removedirs, os.remove, .
. python, :
subprocess.Popen(r'rd /s /q .\ProcessControlSimulator\bin', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
You cannot just copy it one to one. For example, your escape characters () become invalid. In this case, you may need double \.
In addition, there are special API calls to create and destroy directories, look at os.path
Source: https://habr.com/ru/post/1729247/More articles:How to write Cocoa OpenGL application in C ++? - c ++Can a functor save values ββwhen passed to std :: for_each? - c ++Proper MVP implementation with complex controls - asp.netGlassfish cannot find my wsdl when deploying WAR - javaReplacing Class.getSuperclass () with Java ME? - javaimages stretched in Chrome - htmlstructure definition conflict between XS module and perl string - cA PostgreSQL insert that depends on data in another table is best practice? - functionWhat is the advantage of using drupal module_load_include versus just including a file? - drupalHow do you replace page.tpl.php, which is loaded from a module in Drupal? - drupalAll Articles